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

By Waseem Anjum · Published 2026-12-01 · https://fabwaseem.com/blog/postgres-18-docker-volume

> **TL;DR:** On postgres:18 and newer, mount the volume at /var/lib/postgresql, not /var/lib/postgresql/data. The path printed in the startup error shows your case. A new mount path doesn't upgrade old data: use dump and restore into a new volume, or move the files into a version folder and run pg_upgrade --link on a backed-up volume.

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](https://github.com/docker-library/official-images/blob/master/library/postgres) point `latest` at PostgreSQL 19, [released on November 19, 2026](https://www.postgresql.org/docs/19/release-19.html), 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):

```text
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](https://github.com/docker-library/postgres/pull/1259). The data directory became version specific, and the declared volume moved up one level:

| Image | Default `PGDATA` | Mount your volume at |
| --- | --- | --- |
| `postgres:17` and older | `/var/lib/postgresql/data` | `/var/lib/postgresql/data` |
| `postgres:18` | `/var/lib/postgresql/18/docker` | `/var/lib/postgresql` |
| `postgres:19` | `/var/lib/postgresql/19/docker` | `/var/lib/postgresql` |

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.

![Postgres 18 Docker volume layout compared with 17: the mount moves from /var/lib/postgresql/data up to /var/lib/postgresql, with 18/docker inside](https://res.cloudinary.com/wasicloudinary/image/upload/v1796098521/fabwaseem/projects/idrfs59rzfjctxxsry7d.jpg)

## 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](https://github.com/docker-library/postgres/blob/master/18/trixie/docker-entrypoint.sh) 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.

| Path in the error | What it usually means | What to do |
| --- | --- | --- |
| `/var/lib/postgresql/data (unused mount/volume)` | Something is mounted at the old path, but no database sits directly in it or in the other checked folders | Search the volume first, then move the mount to `/var/lib/postgresql` |
| `/var/lib/postgresql/data` | A database is mounted at the old path, typically from 17 or older | Dump and restore, or `pg_upgrade` |
| `/var/lib/postgresql` | The old volume is now mounted at the new path, with database files at its root | Dump and restore, or move the files into `17/docker` and run `pg_upgrade` |
| `/var/lib/postgresql/17/docker` or `18/docker` | A database in the new layout belongs to an older major than the image | Pin the old tag, or run `pg_upgrade` |

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:

```bash
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.

![Postgres 18 Docker error fixes by listed path: unused mount, old data path, parent path with files at root, and older version folder](https://res.cloudinary.com/wasicloudinary/image/upload/v1796098540/fabwaseem/projects/urbel7xdkcb3uvgybqh6.jpg)

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

```yaml
# 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:
```

```bash
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.

```bash
docker compose stop app
```

**2. Dump all databases and roles.** PostgreSQL [recommends the newer version's dump tools](https://www.postgresql.org/docs/18/upgrading.html). Replace `myapp_default` with your Compose network (`docker network ls`) and `your-password` with the database password.

```bash
docker run --rm --network myapp_default -e PGPASSWORD=your-password -v "${PWD}:/backup" postgres:18 pg_dumpall -h db -U postgres -f /backup/dump.sql
```

**3. 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.**

```bash
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.sql
```

**5. Rebuild planner statistics**, which `pg_dumpall` leaves out unless you ask for them.

```bash
docker compose exec db vacuumdb -U postgres --all --analyze-in-stages
```

**6. Check row counts, extensions and logins**, then start the application again.

```bash
docker compose up -d
```

An error saying the `postgres` role already exists is [expected and harmless](https://www.postgresql.org/docs/19/app-pg-dumpall.html). 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`](https://github.com/tianon/docker-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](https://www.postgresql.org/docs/18/release-18.html), 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.

```bash
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.

```bash
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:

```bash
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.**

```bash
docker run --rm -v myapp_pgdata:/var/lib/postgresql -e PGDATAOLD=/var/lib/postgresql/17/docker tianon/postgres-upgrade:17-to-18 --link --check
```

```bash
docker run --rm -v myapp_pgdata:/var/lib/postgresql -e PGDATAOLD=/var/lib/postgresql/17/docker tianon/postgres-upgrade:17-to-18 --link
```

`PGDATAOLD` is needed because the image [defaults to `17/data`](https://github.com/tianon/docker-postgres-upgrade/blob/master/17-to-18/Dockerfile). 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:

```bash
docker run --rm -v myapp_pgdata:/v alpine rm -rf /v/18/docker
```

**5. 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`.

```bash
docker run --rm -v myapp_pgdata:/v alpine cp /v/17/docker/pg_hba.conf /v/18/docker/pg_hba.conf
```

Reapply 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](https://www.postgresql.org/docs/current/pgupgrade.html), and start the rest of the stack:

```bash
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.

![Postgres 17 to 18 upgrade in Docker: back up the volume, move files into 17/docker, check, link upgrade, copy pg_hba.conf, start 18](https://res.cloudinary.com/wasicloudinary/image/upload/v1796098556/fabwaseem/projects/mvyxahn2fh6nv4lcdna5.jpg)

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](https://www.postgresql.org/docs/18/sql-altercollation.html) 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](https://github.com/tianon/docker-postgres-upgrade/blob/master/18-to-19/Dockerfile) 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](https://www.postgresql.org/docs/19/release-19.html). `pg_upgrade` refuses clusters with `btree_gist` indexes on `inet` or `cidr` columns, 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 is `myapp_pgdata18`.
- With 18 running, check `SHOW data_checksums`. If it prints `off`, or your superuser isn't `postgres`, add the flags from step 3 above to both upgrade commands: `-e` options before the image name, `--username=` after `--link`.

First, stop the stack, copy the volume under a new backup name and run the check:

```bash
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 --check
```

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

```bash
docker run --rm -v myapp_pgdata:/v alpine rm -rf /v/19/docker
```

Run the upgrade only after the check passes:

```bash
docker run --rm -v myapp_pgdata:/var/lib/postgresql tianon/postgres-upgrade:18-to-19 --link
```

After it succeeds, copy the access rules:

```bash
docker run --rm -v myapp_pgdata:/v alpine cp /v/18/docker/pg_hba.conf /v/19/docker/pg_hba.conf
```

Reapply 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](https://hub.docker.com/_/postgres) 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](https://github.com/docker/compose/blob/main/pkg/compose/create.go) 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`](https://docs.docker.com/reference/cli/docker/compose/down/) 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](https://github.com/docker-library/postgres/issues/1370) was closed in October 2025.

On a platform such as Coolify, the same mount rule applies in the UI: the [database storage settings](https://coolify.io/docs/databases/configuration/persistent-storage) 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](https://fabwaseem.com/blog/coolify-postgres-backup-cloudflare-r2) keep a copy off the server. Coming from PostgreSQL 14, which is past end of support? The [PostgreSQL 14 upgrade guide](https://fabwaseem.com/blog/postgresql-14-end-of-life-upgrade) covers planning that jump. On 18 you can also move to [UUIDv7 primary keys](https://fabwaseem.com/blog/uuidv7-postgres), and for an app on your own server see [Docker Compose with Cloudflare Tunnel](https://fabwaseem.com/blog/cloudflare-tunnel-docker-compose). The [Postgres Is Enough guide](https://fabwaseem.com/guides/postgres-is-enough) shows what else one database can handle.

## Key takeaways

- On `postgres:18` and newer, mount the volume at `/var/lib/postgresql`.
- The path printed in the error identifies the case; check `PG_VERSION` before 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 over `pg_hba.conf`.
- Pin a major tag such as `postgres:18` so a major upgrade happens only when you choose it.

## Questions

### Where should I mount the volume for Postgres 18 in Docker?

Mount it at /var/lib/postgresql. The official image stores the database in the 18/docker subfolder by default. Version 17 and older images use /var/lib/postgresql/data.

### Why does my postgres:18 container exit immediately?

If the log says there appears to be PostgreSQL data in a listed path, the image found an older database or a mount at the old /var/lib/postgresql/data path and stopped before creating a new database. Read the log with docker compose logs db.

### Can Postgres 18 read a Postgres 17 data directory?

No. A data directory from an older major version has to be upgraded with pg_upgrade, or its contents moved with a dump and restore. Changing the mount path or PGDATA doesn't convert it.

### Does docker compose down -v delete my Postgres data?

It can. The -v flag removes the named volumes declared in the Compose file and anonymous volumes attached to its containers. Back up the database before using it.

### How do I check which Postgres version created a Docker volume?

Read the PG_VERSION file in the data directory. Mount the volume in a temporary Alpine container and search for PG_VERSION, skipping the base folder.
