[Fixed] Postgres container failed to start with initdb error. popen failure: Cannot allocate memory – Postgresql

by
Ali Hasan
amazon-ec2 boot2docker postgresql ubuntu

Quick Fix: Use the ‘postgres:12-bullseye’ container instead of the generic ‘postgres:12’ container.

The Problem:

A Postgres container using the ‘postgres:12’ Docker image fails to start with the following error after a recent project deployment: popen failure: Cannot allocate memory. The issue is specific to this particular container, as another ‘postgresql:12’ container for a different project on the same machine is working fine. Increasing the ‘shmall’ and ‘shmmax’ system parameters, trying newer Postgres images (13.0, 14.0), and restarting/rebooting the instance have not resolved the problem. Using the ‘postgres:11’ image allows the container to run successfully, but downgrading the Postgres version is not a viable solution for production

The Solutions:

Solution 1: Use Postgres Bullseye Variant

The issue arises due to an update in the underlying OS image layer of the Postgres Docker image. The new image uses Debian 12 (bookworm), while the previous one utilized Debian 11 (bullseye). To resolve this, switch to the following container:

postgres:12-bullseye

This variant maintains the bullseye image until the Postgres version reaches its end-of-life or Debian transitions to its Trixie release.

Solution 2: Update Docker Version

The issue arose due to an incompatibility between the Docker version (20.10.8) and the official PostgreSQL 12 image. Updating Docker to a newer version (23.0.6) resolved the problem.

Solution 3: Upgrade the Docker host machine

In this case, upgrading the Docker host machine solved the issue. Specifically, upgrading from image: ubuntu-2004:202010-01 to image: ubuntu-2004:2023.07.1 allowed the use of the latest bookworm Postgres images.

Q&A

Can you use postgres:12 image to avoid errors with postgres:12-bullseye?

Postgres:12 will eventually be EOL, it is recommended to use posgres:12-bullseye.

Is it possible that docker version can be causing the initdb error?

Yes, you can avoid the error by upgrading Docker version.

Can you avoid the initdb error if you update the Circleci machine?

Yes, updating the machine will allow you to use the latest bookworm postgres images.