Postgres container failing with initdb error: `program "postgres" is needed by initdb but was not found`

As of June 15th 2023, the official Postgres Docker images were published to be based on Debian 12.0 (Bookworm) rather than Debian 11.0 (Bullseye).
See Issues · docker-library/postgres · GitHub

CircleCI Support team has noted some customers seeing the following issues when using the official postgres Docker image then:

# error seen when using postgres:13
popen failure: Cannot allocate memory
initdb: error: The program "postgres" is needed by initdb but was not found in the
same directory as "/usr/lib/postgresql/13/bin/initdb".

The Docker community (maintainers of the Postgres official image) are aware of the reported issues.
For folks encountering this error, we recommend try switching to *-bullseye images as a workaround:

- postgres:14
+ postgres:14-bullseye

If this still does not solve your issue, you can also try using alternative Postgres images.
For example, you can consider using CircleCI’s convenience image as well:

- postgres:14
+ cimg/postgres:14

Alternatively, you can also try Bitnami’s Postgres images:

- postgres:14
+ bitnami/postgresql:14

References:

For folks who are interested in confirming the OS version:

$ docker run --rm postgres:13-bullseye cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
$ docker run --rm postgres:13 cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"