Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

The postgres:18 container exits at start because of an old data mount

Your error

Error: in 18+, these Docker images are configured to store database data in a format which is compatible with "pg_ctlcluster" (specifically, using major-version-specific directory names).3 more lines it can show

Fix it fast

Most common cause, 1 of 2

The image moved to 18 while the volume still holds an older cluster

Go back to the major that wrote the data to get the service up, dump every database, then start 18 on a fresh volume mounted at /var/lib/postgresql and load the dump.

Fix
# 1. compose: image: postgres:17, volume still on /var/lib/postgresql/data
docker compose up -d db
docker compose exec -T db pg_dumpall -U postgres > all.sql
docker compose down

# 2. compose: image: postgres:18, new volume
#    volumes:
#      - pgdata18:/var/lib/postgresql
docker compose up -d db
docker compose exec -T db psql -U postgres -d postgres < all.sql
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

From PostgreSQL 18 the official Docker image sets PGDATA to /var/lib/postgresql/18/docker and declares its volume at /var/lib/postgresql. When it finds old data or a mount at /var/lib/postgresql/data, the entrypoint prints "in 18+, these Docker images are configured to store database data..." and exits, so your app gets connection refused. Mount the volume at /var/lib/postgresql, or dump and restore.

  • Applies to postgres official Docker image 18 and later, Docker Compose v2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Read the last lines the database container printed before it stopped. The path listed under "there appears to be PostgreSQL data in" and whether it carries the words unused mount/volume tell the two causes apart.

Run this first
docker compose logs --tail 30 db

Then find the line you see in this list and open its fix:

  1. 1The image moved to 18 while the volume still holds an older clusterThe log lists /var/lib/postgresql/data without (unused mount/volume), and the compose file says postgres:latest or was just bumped from 17
  2. 2A new 18 setup copies the volume line from a 17-era compose fileThe log shows /var/lib/postgresql/data (unused mount/volume), even though the volume is new and empty

What does the error look like?

  • Error: in 18+, these Docker images are configured to store database data in a format which is compatible with "pg_ctlcluster" (specifically, using major-version-specific directory names).
  • Counter to that, there appears to be PostgreSQL data in:
  • /var/lib/postgresql/data (unused mount/volume)
  • This is usually the result of upgrading the Docker image without upgrading the underlying database using "pg_upgrade" (which requires both versions).

Cause 1 of 2

The image moved to 18 while the volume still holds an older cluster

You seeThe log lists /var/lib/postgresql/data without (unused mount/volume), and the compose file says postgres:latest or was just bumped from 17

Check

The 18+ entrypoint refuses to start when it finds a PG_VERSION file from an earlier cluster in the old location. That data was written by an older major and needs pg_upgrade (with both versions present) or a dump and restore first, so the entrypoint stops rather than initialise a new, empty cluster at the 18 path beside it.

Check
docker volume ls
docker run --rm -v myapp_pgdata:/data busybox cat /data/PG_VERSION

Fix

Go back to the major that wrote the data to get the service up, dump every database, then start 18 on a fresh volume mounted at /var/lib/postgresql and load the dump. Keep the old volume until the new one is checked.

Fix
# 1. compose: image: postgres:17, volume still on /var/lib/postgresql/data
docker compose up -d db
docker compose exec -T db pg_dumpall -U postgres > all.sql
docker compose down

# 2. compose: image: postgres:18, new volume
#    volumes:
#      - pgdata18:/var/lib/postgresql
docker compose up -d db
docker compose exec -T db psql -U postgres -d postgres < all.sql

Verify

The container stays up, reports the new major and lists your databases.

Verify
docker compose exec db psql -U postgres -c "SELECT version();"
docker compose exec db psql -U postgres -l

Confirmed by github.com

Did this fix it?

Cause 2 of 2

A new 18 setup copies the volume line from a 17-era compose file

You seeThe log shows /var/lib/postgresql/data (unused mount/volume), even though the volume is new and empty

Check

On a fresh install the same exit comes from the mount path alone. The image documentation for 18 and later says mounts should target /var/lib/postgresql; a volume on /var/lib/postgresql/data is not where 18 writes its files, so the entrypoint flags it as unused rather than let you believe that volume holds your database.

Check
docker compose config | grep -n -A3 'volumes:'

Fix

Point the volume at /var/lib/postgresql. The server then writes into the version folder 18/docker inside it, which leaves room for a later pg_upgrade --link.

Fix
services:
  db:
    image: postgres:18
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - pgdata:/var/lib/postgresql
volumes:
  pgdata:

Verify

The data directory is the version-specific path inside the mounted volume.

Verify
docker compose exec db sh -c 'echo $PGDATA'

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Pin the major in the image tag (postgres:18, never postgres:latest), so a pull cannot switch versions under an existing volume.
  • Treat a major bump as a data migration: take a pg_dumpall before changing the tag, and test the restore on a copy first.
  • Keep scripts in /docker-entrypoint-initdb.d idempotent and run migrations separately; the image runs them only on a first start with nothing in the data folder, so a restored or reused volume skips them without a word.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.