By ProDevs
Published 2026-11-03
AI disclosure: drafted with AI assistance. Every command and output below comes from our own run on 3 November 2026.
Who this is for: developers and DevOps engineers · Intermediate · You’ll need: Postgres 17 running from the official image in Docker or Compose, root on the host, free disk at least the size of your data, and a maintenance window
Short answer
The postgres:18 image keeps data in /var/lib/postgresql/18/docker and declares its volume at /var/lib/postgresql, not /var/lib/postgresql/data. Started on a Postgres 17 volume, it exits with code 1 instead of upgrading. Move the data with pg_dumpall and restore, or with pg_upgrade --link, then mount the volume at /var/lib/postgresql.
On our test machine, a 307 MB database (2,000,000 rows) restored from a dump in 2.3 seconds, and pg_upgrade --link finished in 2.4 seconds. Both copies returned the same row count and the same checksum as the 17 source.
How we tested: a disposable test machine with 8 vCPU, 15 GB RAM and an ext4 disk, Debian 13, Docker Engine 26.1.5 and Docker Compose 2.26.1. Images: postgres:17.11 (digest sha256:3b0cda93be7c…), postgres:18.6 (sha256:3a4264520e12…) and tianon/postgres-upgrade:17-to-18 (sha256:82e3638e4226…, which ships PostgreSQL 18.6). The dataset was pgbench -i -s 20. This was a test box, not a production server, and your timings will scale with your data size and disk.
What changed in postgres:18 (and why your container exits)
Up to 17, the official image set PGDATA and its VOLUME to /var/lib/postgresql/data. From 18, PGDATA is /var/lib/postgresql/18/docker and the VOLUME is /var/lib/postgresql. The image docs say the change lets pg_upgrade --link run without crossing a mount boundary, and that 19 will use /var/lib/postgresql/19/docker (postgres image docs). We confirmed both values with docker image inspect on the pinned images.
The image does not upgrade your data. PostgreSQL itself says a move to 18 from any earlier major needs a dump and restore, pg_upgrade, or logical replication (PostgreSQL 18 release notes).
When we started postgres:18.6 on a copy of a 17 volume still mounted at /var/lib/postgresql/data, it printed this and exited with code 1:
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.
...
Counter to that, there appears to be PostgreSQL data in:
/var/lib/postgresql/data
Two more cases gave the same error with a different path line:
- The 17 files mounted straight at
/var/lib/postgresql: the line names/var/lib/postgresql. - An empty volume at
/var/lib/postgresql/data: the line reads/var/lib/postgresql/data (unused mount/volume). So even a fresh 18 container fails if your Compose file still has the old mount path.
Pin the major version in your image tag. If you use a tag with no version, the next major release will hit this again.
Prerequisites
- Free disk. Path A needs room for the dump file plus the new cluster. Path B in link mode needs little extra space, but old and new data must sit on one filesystem, which a single volume gives you (pg_upgrade docs).
- The current image tag, the Compose file, and the volume name or bind path.
- A maintenance window. Both paths stop writes.
sudoor root on the host for the volume moves. The commands below assume you rundockerwith enough rights.
Step 1: Record the 17 setup and take a backup
docker compose exec db psql -U postgres -Atc 'select version()' # tested on Docker Engine v26.1.5
docker compose exec db psql -U postgres -Atc 'show data_checksums'
docker inspect "$(docker compose ps -q db)" \
--format '{{range .Mounts}}{{.Type}} {{.Name}} -> {{.Destination}}{{end}}'
docker compose exec -T db pg_dumpall -U postgres > dumpall-17.sql
On our run the checks printed:
PostgreSQL 17.11 (Debian 17.11-1.pgdg13+2) on x86_64-pc-linux-gnu, ...
off
volume pg17data -> /var/lib/postgresql/data
data_checksums is off on a default 17 cluster. Remember that value. It matters in Step 3.
Save a row count and a checksum of one important table so you can prove the data survived. We used:
docker compose exec db psql -U postgres -d bench -Atc \
"select count(*), md5(string_agg(aid::text||abalance::text, ',' order by aid)) from pgbench_accounts"
2000000|f3a0c9014f4c372c02213e482a5fc8d7
The dump file was 194 MB and took under a second here. Keep it until the upgrade is verified, whichever path you pick.
Step 2 (Path A): Dump from 17, restore into 18
This path is the simplest and works on any layout. Stop the app first so nothing writes after the dump.
docker compose stop db
docker volume create pg18data
docker run -d --name pg18 -e POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \
-v pg18data:/var/lib/postgresql postgres:18.6
docker exec -i pg18 psql -U postgres -f - < dumpall-17.sql
The restore printed one error, ERROR: role "postgres" already exists. That is expected: the new container already made the postgres role. Anything else in the output needs a look.
The restore took 2.3 seconds on our 307 MB database. The new cluster reported data_checksums as on, because PostgreSQL 18 initdb turns checksums on by default (release notes). The row count and checksum matched Step 1.
Step 3 (Path B): pg_upgrade –link in place
This path skips the dump and is faster on big databases. It reuses the 17 data files, so take the Step 1 backup first.
First move the 17 files into a 17/data subdirectory of the same volume. That is the old-cluster path the tianon/postgres-upgrade:17-to-18 image expects by default. Its README calls the image a proof of concept, so test on a copy first (tianon/docker-postgres-upgrade).
docker compose stop db
docker run --rm -v myproject_pgdata:/var/lib/postgresql alpine:3.22 sh -c \
'cd /var/lib/postgresql && mkdir -p 17/data && find . -mindepth 1 -maxdepth 1 ! -name 17 -exec mv {} 17/data/ \;'
Our first upgrade attempt failed after 0.7 seconds:
Checking cluster versions ok
old cluster does not use data checksums but the new one does
Failure, exiting
pg_upgrade needs matching checksum settings, and 18’s initdb now turns them on. Create the new cluster without checksums using --no-data-checksums (release notes). The image passes POSTGRES_INITDB_ARGS to initdb. Remove the half-made 18 directory before you retry:
docker run --rm -v myproject_pgdata:/var/lib/postgresql alpine:3.22 sh -c 'cd /var/lib/postgresql && rm -r 18'
docker run --rm -e POSTGRES_INITDB_ARGS=--no-data-checksums \
-v myproject_pgdata:/var/lib/postgresql tianon/postgres-upgrade:17-to-18 --link
That run ended with Upgrade Complete in 2.4 seconds. The volume then held 17/, 18/docker/ and delete_old_cluster.sh.
Copy your old pg_hba.conf across before you start 18. This is the step that is easy to miss. The new cluster comes from a plain initdb, so its pg_hba.conf only allows local and loopback connections. The 17 file from the official image also had host all all all scram-sha-256. Without it, our first connection from another container failed with:
FATAL: no pg_hba.conf entry for host "172.17.0.4", user "postgres", database "postgres", no encryption
docker run --rm -v myproject_pgdata:/var/lib/postgresql alpine:3.22 \
cp -p /var/lib/postgresql/17/data/pg_hba.conf /var/lib/postgresql/18/docker/pg_hba.conf
After the copy, the same connection worked. If you changed postgresql.conf on 17, carry those settings over by hand too. Don’t copy the whole file blindly between major versions.
Once 18 is running and verified, delete the old cluster with the generated delete_old_cluster.sh, or by removing the 17 directory. With --link, the old cluster can’t be safely started after the new one has run. The pg_upgrade output says so.
Step 4: Change the Compose file
The change for both paths is the image tag and the mount target:
services:
db:
- image: postgres:17.11
+ image: postgres:18.6
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- - pgdata:/var/lib/postgresql/data
+ - pgdata:/var/lib/postgresql
For Path A, point pgdata at the new volume, or restore into the old one after you empty it. We ran Path B end to end with Compose: 17 up, data loaded, stop, restructure, upgrade, copy pg_hba.conf, edit the file as above, then docker compose up -d. The container started on 18.6 with all 200,000 test rows present.
Verify
docker compose exec db psql -U postgres -Atc 'select version()'
docker compose exec db psql -U postgres -d bench -Atc \
"select count(*), md5(string_agg(aid::text||abalance::text, ',' order by aid)) from pgbench_accounts"
Expected output: the version line starts PostgreSQL 18.6, and the count and checksum match Step 1. Both of our paths returned 2000000|f3a0c9014f4c372c02213e482a5fc8d7.
After Path B, refresh planner statistics. pg_upgrade printed this suggestion, and --missing-stats-only is new in 18 (release notes):
docker compose exec db vacuumdb -U postgres --all --analyze-in-stages --missing-stats-only
Troubleshooting
- Exit code 1 with “there appears to be PostgreSQL data in /var/lib/postgresql/data”: the mount still points at the 17 path. Change it to
/var/lib/postgresqland upgrade the data first. - “(unused mount/volume)” on a new install: same fix. An empty volume at the old path also stops 18.
- “old cluster does not use data checksums but the new one does”: recreate the new cluster with
POSTGRES_INITDB_ARGS=--no-data-checksums, after removing the18directory. - “no pg_hba.conf entry for host” after Path B: copy the 17
pg_hba.confinto18/docker, then restart. - Path A restore shows
role "postgres" already exists: expected, ignore it.
Rollback
- Path A: the 17 volume is untouched. Put
postgres:17.11and/var/lib/postgresql/databack in Compose and start it. - Path B before 18 has started: the old cluster is still usable.
pg_upgraderenamedglobal/pg_controltopg_control.old. Rename it back, move the files out of17/datato the volume root, and start 17 again. - Path B after 18 has started: don’t start 17 on those files. Restore
dumpall-17.sqlinto a freshpostgres:17.11volume.
FAQ
Why won’t postgres:18 start on my Postgres 17 volume?
From 18 the official image keeps data in /var/lib/postgresql/18/docker and checks for data at the old /var/lib/postgresql/data path. When it finds data there, or even an empty mount, it prints an error and exits with code 1. It never upgrades data for you.
Where should I mount the volume for postgres:18?
Mount one volume at /var/lib/postgresql. The image creates 18/docker inside it. Keeping every major version under one mount is what lets pg_upgrade --link work.
pg_dump or pg_upgrade for a Docker Postgres 17 to 18 upgrade?
Use pg_dumpall and restore if your database is small or you want the 17 volume left alone. Use pg_upgrade --link for large databases, where a dump takes too long. It reuses the data files, so back up first. On our 307 MB test both took between 2 and 3 seconds.
Why does pg_upgrade say the old cluster does not use data checksums?
PostgreSQL 18 initdb turns data checksums on by default, and pg_upgrade needs both clusters to match. A default 17 cluster has them off. Create the 18 cluster with --no-data-checksums.
Sources
- PostgreSQL 18 release notes: upgrade methods,
initdbchecksum default,--no-data-checksums,vacuumdb --missing-stats-only. Checked 2026-11-03. - pg_upgrade documentation (18): link mode and the same-filesystem requirement. Checked 2026-11-03.
- Docker official postgres image docs:
PGDATAandVOLUMEchange in 18. Checked 2026-11-03. - docker-library/postgres pull request 1259: the change itself, linked from the error message. Checked 2026-11-03.
- tianon/docker-postgres-upgrade: the upgrade image used in Path B. Checked 2026-11-03.
- Docker Hub: postgres official image: the 17 and 18 tags used here. Checked 2026-11-03.
- ProDevs test run, 3 November 2026: all outputs, timings and checksums above.
Related on ProDevs: Postgres in Docker idle RAM, Docker volume vs bind mount, Docker container keeps restarting.
Need a hand with a Postgres upgrade or a Docker setup that keeps breaking? See ProDevs services.


