Postgres 18 Docker Volume Error: Fix It Without Losing Data
The postgres:18 image moved its data directory, so old volume mounts stop the container. Read the path in the error, pick the right fix and keep your data.

On this page
The Postgres 18 Docker volume error means the image found a mount or old data at a path it no longer uses. For a new database, mount your volume at /var/lib/postgresql instead of /var/lib/postgresql/data. If the volume already holds Postgres 17 or older data, changing the path isn't enough: upgrade the data with a dump and restore or pg_upgrade.
This applies to the official postgres image for 18 and 19. The current image tags point latest at PostgreSQL 19, released on November 19, 2026, so an unpinned image: postgres can hit this on its next pull.
What does the Postgres 18 Docker volume error mean?#
It means the container refused to create a new, empty database because it saw signs of an existing one, or a mount at the old data path. The check runs before initdb, so no new cluster has been created. The message looks like this (trimmed):
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)The cause is a change to the official image in version 18. The data directory became version specific, and the declared volume moved up one level:
The point of the new layout is upgrades. With old and new version folders under one mount, pg_upgrade --link can hard-link files between them instead of copying.

Which fix do you need?#
Read the path printed under "there appears to be PostgreSQL data in", because it tells you which situation you're in. The image's entrypoint script looks for a PG_VERSION file in /var/lib/postgresql, /var/lib/postgresql/data and any /var/lib/postgresql/*/docker folder. If it finds none, it checks for a mount at the old path.
The last row is why the same error comes back when an 18 container is bumped to 19.
Before changing anything, see what the volume holds. Find its name with docker volume ls (Compose prefixes it with the project name, such as myapp_pgdata). Type it exactly, because docker run creates a new empty volume for an unknown name. Then run:
docker run --rm -v myapp_pgdata:/v alpine find /v -name PG_VERSION ! -path '*/base/*' -exec grep -H . {} +A result such as /v/PG_VERSION:17 means a version 17 database sits at the root of the volume. No output means no database was found there.
"Unused mount/volume" does not prove the volume is empty. If you used to set PGDATA to a subfolder such as /var/lib/postgresql/data/pgdata, the search prints /v/pgdata/PG_VERSION. Don't just move the mount then, because 18 won't look there and will create an empty database. For a dump, run the old tag with the old mount and that PGDATA value. For pg_upgrade, use the subfolder command in step 2 below.

How do you fix it for a new database?#
Mount the volume at /var/lib/postgresql and recreate the container. Use this only when the PG_VERSION search finds nothing you care about.
# compose.yaml
services:
db:
image: postgres:18
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -h 127.0.0.1"]
interval: 5s
retries: 10
volumes:
pgdata:docker compose up -d --wait db
docker compose exec db psql -U postgres -c "SHOW data_directory"The second command should print /var/lib/postgresql/18/docker. The -h 127.0.0.1 in the health check is deliberate: during first-time setup the entrypoint runs a temporary server that doesn't listen on TCP, so the check doesn't pass until the real server is up.
For a bind mount, the rule is the same: ./postgres:/var/lib/postgresql. With plain docker run, use -v pgdata:/var/lib/postgresql.
How do you upgrade existing Postgres 17 data with dump and restore?#
Run the old container, dump everything with the new version's pg_dumpall, and restore into a new volume. It is the simplest route for small and medium databases, and the old volume stays untouched as your rollback. The commands assume a Compose file with a database service named db and an application service named app.
If you already changed the image tag, put back the old tag and the old mount first, and start it with docker compose up -d db. Then:
1. Stop whatever writes to the database, keeping the database running.
docker compose stop app2. Dump all databases and roles. PostgreSQL recommends the newer version's dump tools. Replace myapp_default with your Compose network (docker network ls) and your-password with the database password.
docker run --rm --network myapp_default -e PGPASSWORD=your-password -v "${PWD}:/backup" postgres:18 pg_dumpall -h db -U postgres -f /backup/dump.sql3. Change compose.yaml to postgres:18 with a new volume name, such as pgdata18:/var/lib/postgresql. Declare it under volumes:, delete any PGDATA line and add the health check shown above.
4. Start the new server and restore.
docker compose up -d --wait db
docker compose cp dump.sql db:/tmp/dump.sql
docker compose exec db psql -X -U postgres -d postgres -f /tmp/dump.sql5. Rebuild planner statistics, which pg_dumpall leaves out unless you ask for them.
docker compose exec db vacuumdb -U postgres --all --analyze-in-stages6. Check row counts, extensions and logins, then start the application again.
docker compose up -dAn error saying the postgres role already exists is expected and harmless. A message that a database created by POSTGRES_DB already exists is expected too. Read every other error. If your superuser isn't postgres, set POSTGRES_USER on the new service and use that name in the health check and with -U.
Writing the dump with -f rather than a shell redirect also avoids encoding surprises in Windows shells. Extensions such as pgvector must be present in the new database image before you restore.
How do you run pg_upgrade --link in Docker?#
Move the old files into a 17/docker folder inside the same volume, then run an image that contains both versions. The tianon/postgres-upgrade images do this; their author describes them as a proof of concept, so rehearse on a copy of the volume. For 16 data, use the 16-to-18 tag and replace 17 with 16 in the folder paths. Run the commands one at a time and stop at the first failure.
If you already changed the image tag, put back the old tag and the old mount, and run docker compose up -d db before step 1.
1. Check the checksum setting, then stop the containers and copy the volume. PostgreSQL 18 enables data checksums by default, and pg_upgrade requires both clusters to match. Note what the old server prints. --link makes the old cluster unsafe to start once the new one has run, so the volume copy is your way back.
docker compose exec db psql -U postgres -c "SHOW data_checksums"
docker compose stop -t 120
docker compose down
docker run --rm -v myapp_pgdata:/from -v pgdata_backup:/to alpine cp -a /from/. /to/2. Move the files into the version folder. If PG_VERSION sits at the root of the volume, run this. It skips any 18 or 19 folder a failed start may have left.
docker run --rm -v myapp_pgdata:/v alpine sh -c 'mkdir -p /v/17/docker && find /v -mindepth 1 -maxdepth 1 ! -name 17 ! -name 18 ! -name 19 -exec mv {} /v/17/docker/ \;'If your files are in the pgdata subfolder instead, run this one in its place:
docker run --rm -v myapp_pgdata:/v alpine sh -c 'mkdir -p /v/17 && mv /v/pgdata /v/17/docker'Skip both if they are already in 17/docker.
3. Match the flags. If the checksum query printed off, add -e POSTGRES_INITDB_ARGS=--no-data-checksums to both commands in the next step. Skipping it ends in old cluster does not use data checksums but the new one does. If the old superuser isn't postgres (say appuser), add -e POSTGRES_INITDB_ARGS=--username=appuser, put --username=appuser after --link, and use -U appuser in every psql and vacuumdb command. When you need both initdb flags, quote them in one variable: -e POSTGRES_INITDB_ARGS="--no-data-checksums --username=appuser". A cluster created with a custom locale or encoding needs those initdb options there too.
4. Run the check, then run the upgrade only if the check passes.
docker run --rm -v myapp_pgdata:/var/lib/postgresql -e PGDATAOLD=/var/lib/postgresql/17/docker tianon/postgres-upgrade:17-to-18 --link --checkdocker run --rm -v myapp_pgdata:/var/lib/postgresql -e PGDATAOLD=/var/lib/postgresql/17/docker tianon/postgres-upgrade:17-to-18 --linkPGDATAOLD is needed because the image defaults to 17/data. The check run already initializes the new cluster, so don't start postgres:18 until the real upgrade finishes. If you initialized it with the wrong flags during this 17 to 18 upgrade, remove the new folder and repeat:
docker run --rm -v myapp_pgdata:/v alpine rm -rf /v/18/docker5. Carry over the client access rules after the upgrade succeeds. The new cluster has a fresh pg_hba.conf without the host all all all line the entrypoint normally appends, so apps can get no pg_hba.conf entry for host.
docker run --rm -v myapp_pgdata:/v alpine cp /v/17/docker/pg_hba.conf /v/18/docker/pg_hba.confReapply custom settings from the old postgresql.conf and postgresql.auto.conf (where ALTER SYSTEM writes) as well.
6. Start 18 and refresh statistics. In compose.yaml, set image: postgres:18, mount pgdata:/var/lib/postgresql and delete any PGDATA line, since a leftover value points 18 at a different folder. Then run the commands pg_upgrade recommends, and start the rest of the stack:
docker compose up -d --wait db
docker compose exec db vacuumdb -U postgres --all --analyze-in-stages --missing-stats-only
docker compose exec db vacuumdb -U postgres --all --analyze-only
docker compose up -d--wait relies on the health check from the Compose file above, so add it if yours has none. If pg_upgrade reported that it created SQL scripts, such as one for extension updates, run them as it instructs.

If you need to go back after 18 has started, don't reuse the upgraded volume. Restore the old tag and old mount, and point them at pgdata_backup, declared in Compose with external: true.
Extensions such as pgvector need their libraries in both the new runtime image and the upgrade image, so build your own upgrade image if the check reports missing libraries. The upgrade images are Debian based, so if you run an Alpine image, use dump and restore. If Postgres reports a collation version mismatch after an operating system change, run REINDEX DATABASE first and only then ALTER DATABASE yourdb REFRESH COLLATION VERSION.
How do you upgrade Postgres 18 to 19 in the same volume?#
If your 18 database already uses the new layout, the 18-to-19 image already expects 18/docker and 19/docker, so no path variables are needed. Not ready? Set image: postgres:18 before running anything here, and the error disappears because the data and image match again. After a --link upgrade and a 19 start, going back means restoring the volume copy.
Prepare before you stop anything:
- Read the PostgreSQL 19 migration notes.
pg_upgraderefuses clusters withbtree_gistindexes oninetorcidrcolumns, JIT is now off by default, and RADIUS authentication is gone. - Use the volume that holds your 18 data. The commands say
myapp_pgdata, but if you moved to 18 by dump and restore, yours ismyapp_pgdata18. - With 18 running, check
SHOW data_checksums. If it printsoff, or your superuser isn'tpostgres, add the flags from step 3 above to both upgrade commands:-eoptions before the image name,--username=after--link.
First, stop the stack, copy the volume under a new backup name and run the check:
docker compose stop -t 120
docker compose down
docker run --rm -v myapp_pgdata:/from -v pgdata18_backup:/to alpine cp -a /from/. /to/
docker run --rm -v myapp_pgdata:/var/lib/postgresql tianon/postgres-upgrade:18-to-19 --link --checkIf the check ran with the wrong flags, remove only the new 19/docker folder and check again. Never remove 18/docker here, because it is your live data.
docker run --rm -v myapp_pgdata:/v alpine rm -rf /v/19/dockerRun the upgrade only after the check passes:
docker run --rm -v myapp_pgdata:/var/lib/postgresql tianon/postgres-upgrade:18-to-19 --linkAfter it succeeds, copy the access rules:
docker run --rm -v myapp_pgdata:/v alpine cp /v/18/docker/pg_hba.conf /v/19/docker/pg_hba.confReapply your custom settings, change the tag to postgres:19, keep the same parent mount, then run the commands from step 6.
Why does Postgres 18 start with an empty database?#
The usual reason is that the container is reading a different volume or folder from the one holding your data. The startup check can't see a volume that isn't mounted.
A common source is an old setup that mounted a volume at /var/lib/postgresql on 17 or earlier. The image documentation warns that this does not persist data across container re-creation: the files went into an anonymous volume created for /var/lib/postgresql/data. Run docker volume ls, then use the PG_VERSION search on the volumes with long random names.
The same can happen to a Compose file with no volume line. When Compose recreates a container, it reuses an anonymous volume at a path the new image or service still declares. The 18 image no longer declares /var/lib/postgresql/data, so a 17 anonymous volume can sit unused while 18 starts empty. Avoid cleanup commands until you've found it: docker compose down -v removes the named volumes in your Compose file and attached anonymous volumes.
Can you keep the old /var/lib/postgresql/data path?#
Yes, by setting PGDATA=/var/lib/postgresql/data explicitly, which the image maintainers note works on old and new images. It doesn't upgrade anything: Postgres 18 pointed at 17 files stops with database files are incompatible with server.
It also switches off the old-data check, which only runs when PGDATA has its default value, and you give up the single-mount layout that makes --link easy. Use it for a tool that hardcodes the path, not as the default for new projects. If an early 18 image fails with error mounting ... no such file or directory on the old path, pull a current one; that report was closed in October 2025.
On a platform such as Coolify, the same mount rule applies in the UI: the database storage settings list /var/lib/postgresql as the destination path for PostgreSQL 18 and later.
What should you set up after the upgrade?#
Set up backups, then pin the major tag. On Coolify, scheduled Postgres backups to Cloudflare R2 keep a copy off the server. Coming from PostgreSQL 14, which is past end of support? The PostgreSQL 14 upgrade guide covers planning that jump. On 18 you can also move to UUIDv7 primary keys, and for an app on your own server see Docker Compose with Cloudflare Tunnel. The Postgres Is Enough guide shows what else one database can handle.
Key takeaways#
- On
postgres:18and newer, mount the volume at/var/lib/postgresql. - The path printed in the error identifies the case; check
PG_VERSIONbefore changing anything. - A new mount path never upgrades data. Use dump and restore, or
pg_upgrade. - For
--link, copy the volume first, match checksum settings and carry overpg_hba.conf. - Pin a major tag such as
postgres:18so a major upgrade happens only when you choose it.
Questions
Share this guide



Comments
No comments yet. Questions, corrections and your own results are all welcome.
Be kind and stay on topic. No name needed: leave it blank and you post under an anonymous name that stays yours. Links get
nofollow; spam is removed. Your email is never shown and is only used for reply notifications you ask for. Privacy