Error: in 18+, these Docker images are configured to store database data in a format which is compatible with "pg_ctlcluster" — and the container stops. The postgres:18 image has moved its data directory, and your compose file is still mounting the old one.

Before 18, the official image kept the cluster at /var/lib/postgresql/data and everyone mounted a volume there. From 18 the data lives in a major-version subdirectory, /var/lib/postgresql/18/docker, and the entrypoint refuses to start when something is mounted at the old path. Mount the volume one level up, at /var/lib/postgresql, and the image puts its data inside it. Whether anything can be lost depends on one line of the message, explained below.

The error

PS> docker run -d --name fix5-pg-u3 -e POSTGRES_PASSWORD=fix5 -v fix5-pg-fresh:/var/lib/postgresql/data postgres:18
PS> docker ps -a --filter name=fix5-pg-u3 --format "{{.Names}} {{.Status}}"
fix5-pg-u3 Exited (1) 14 seconds ago

PS> docker logs fix5-pg-u3
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).  This better reflects how
       PostgreSQL itself works, and how upgrades are to be performed.

       See also https://github.com/docker-library/postgres/pull/1259

       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).

       The suggested container configuration for 18+ is to place a single mount
       at /var/lib/postgresql which will then place PostgreSQL data in a
       subdirectory, allowing usage of "pg_upgrade --link" without mount point
       boundary issues.

       See https://github.com/docker-library/postgres/issues/37 for a (long)
       discussion around this process, and suggestions for how to do so.

That is the message with a brand-new, empty named volume at the old path, the case of a fresh project started from a pre-18 compose file. A volume that really holds a PostgreSQL 16 database prints the same text with the path alone:

       Counter to that, there appears to be PostgreSQL data in:
         /var/lib/postgresql/data

Why it happens

The change is deliberate. PostgreSQL's own tooling (pg_ctlcluster, pg_upgrade --link) expects one cluster directory per major version, so an upgrade can lay 19 next to 18 inside the same volume and hard-link the files across. A volume mounted at /var/lib/postgresql/data makes that impossible: the next version's directory would be outside the mount.

So the 18 image's default PGDATA became /var/lib/postgresql/18/docker, and the entrypoint gained a guard. When that directory holds no PG_VERSION file, the script looks for one under /var/lib/postgresql, under the old /var/lib/postgresql/data and under any */docker folder, and stops with the message above rather than silently initialise a fresh, empty cluster beside your real data. It reads as "worked forever, broke today" because postgres:18 is a moving tag — nothing in the compose file changed; the image underneath it did.

Read the line under "Counter to that". When it ends in (unused mount/volume), there is no old database at all: the entrypoint found no PG_VERSION anywhere and only noticed that something is mounted at /var/lib/postgresql/data. That is the guard's own fallback, in the image's docker-entrypoint.sh:

		for d in /var/lib/postgresql /var/lib/postgresql/data /var/lib/postgresql/*/docker; do
			if [ -s "$d/PG_VERSION" ]; then
				OLD_DATABASES+=( "$d" )
			fi
		done
		...
			OLD_DATABASES+=( '/var/lib/postgresql/data (unused mount/volume)' )

In the test, a brand-new named volume at the old path printed exactly the block above, the volume was still empty afterwards, and the same volume mounted at /var/lib/postgresql started straight away. Nothing is lost by moving the mount.

When the path has no such note, it does hold a database, made by an older major version. A volume initialised by postgres:16 printed the plain path, and moving that volume up to /var/lib/postgresql does not get past the guard either; the message then names /var/lib/postgresql. Moving the mount alone would at best start a fresh, empty 18 cluster beside the old files.

The fix

The one-line change in docker-compose.yml, with the reason committed beside it:

    volumes:
      # postgres:18 images keep data in a major-version subdirectory
      # (/var/lib/postgresql/18/docker), so the volume mounts one level up.
      # Mounting at /var/lib/postgresql/data — the pre-18 habit — makes the
      # container refuse to start.
      - cliniclive-pgdata:/var/lib/postgresql

Then docker compose down and docker compose up -d. The same change as a docker run, on the empty volume of the test, started the 18 image at once:

PS> docker run -d --name fix5-pg-f1 -e POSTGRES_PASSWORD=fix5 -v fix5-pg-fresh:/var/lib/postgresql postgres:18
PS> docker exec fix5-pg-f1 psql -U postgres -c "SHOW data_directory;" -c "SELECT version();"
        data_directory
-------------------------------
 /var/lib/postgresql/18/docker
(1 row)

If the volume holds a real 16 database, keep running the old tag on it until the data has moved: postgres:16 on that volume at its own path started and returned its row, and database files are incompatible with server shows the dump-and-restore and pg_upgrade routes. If you must keep the old layout, the 18 image still honours an explicit PGDATA=/var/lib/postgresql/data: on a fresh volume it started and SHOW data_directory answered /var/lib/postgresql/data, at the cost of the in-place upgrade story later. Docker then also creates an anonymous volume at /var/lib/postgresql, because the image declares one there.

Going back to postgres:16

The other direction does not give the "incompatible" message you might expect. Three ways of pointing postgres:16 at a volume that postgres:18 had filled gave three outcomes. Mounted at /var/lib/postgresql/data, it stops with initdb: error: directory "/var/lib/postgresql/data" exists but is not empty, because the 18 cluster sits in an 18/docker folder there. Pointed at that folder with PGDATA, it stops on a setting that 16 does not know:

PostgreSQL Database directory appears to contain a database; Skipping initialization

2026-10-22 12:47:35.645 GMT [1] LOG:  unrecognized configuration parameter "autovacuum_worker_slots" in file "/var/lib/postgresql/18/docker/postgresql.conf" line 687
2026-10-22 12:47:35.645 GMT [1] FATAL:  configuration file "/var/lib/postgresql/18/docker/postgresql.conf" contains errors

Mounted at /var/lib/postgresql, it starts without a word and creates a new, empty 16 cluster in a data folder beside the 18 one; your tables are simply not there:

PS> docker exec fix5-pg-u5 ls -l /var/lib/postgresql /var/lib/postgresql/18
/var/lib/postgresql:
total 8
drwxr-xr-x  3 postgres root     4096 Oct 22 12:43 18
drwx------ 19 postgres postgres 4096 Oct 22 12:47 data

The image tag decides which server reads the volume, so change its major version only together with a data migration.

Where it bit us

Season three, Part 3: the app needs a door, the first docker compose up of the season. The compose file dated from season one's Part 5 and had not been touched since; the fix is in tag pocket-03 of the repo. The fresh database also had to re-seed the demo codes for today — demo data has a shelf life, a season-two lesson back for a second visit. The lesson: a major-version tag is not a pin.

How it was reproduced

The captures above are from October 22, 2026: Docker Desktop 4.95.0 (engine 29.8.2) on Windows 11, Windows PowerShell 5.1, images postgres:18 (18.4) and postgres:16 (16.14). One named volume was initialised by postgres:16 with a one-row table, one by postgres:18, and one was created empty by the failing run itself; each was then mounted at both paths on both images, and the empty one once more with PGDATA set to the old path. Names beginning fix5- are this test's own containers and volumes, all removed afterwards. The post first quoted the message from the image's entrypoint script; the block above is the captured output.

Frequently asked

Why does the postgres:18 Docker container exit immediately with a volume at /var/lib/postgresql/data?
Because PostgreSQL 18 images store the cluster in a major-version subdirectory, /var/lib/postgresql/18/docker, and the entrypoint refuses to start when something sits on the old /var/lib/postgresql/data mount, a database from an older version or just an empty volume. Change the volume to mount at /var/lib/postgresql instead.
Where should the Docker volume be mounted for PostgreSQL 18?
At /var/lib/postgresql, one level above the old path. The image then creates its data directory inside the volume, and future major-version upgrades with pg_upgrade --link can place the next version alongside it in the same mount.
Will I lose my data when I change the mount path for postgres:18?
Look at the path the message names. If it ends in (unused mount/volume), the old mount is empty and there is nothing to lose; mount the volume at /var/lib/postgresql and the 18 image starts. If it names /var/lib/postgresql/data without that note, it holds an older major version's database: keep the old image running on it until you have moved the data with a dump and restore or pg_upgrade.

More decoded errors in the Fixes category; the series that wrote this compose file starts at Part 1.