From version 18 the official Postgres image stores data under /var/lib/postgresql/18/docker. It treats a volume on the old path /var/lib/postgresql/data as foreign and refuses to start – Compose only says “unhealthy”.
Symptom
docker compose up -d --wait; echo "Exit: $?" Container notes-db-1 Error dependency db failed to start dependency failed to start: container notes-db-1 is unhealthy Exit: 1 db Restarting (1) Less than a second ago
The db block was copied from an older guide. All other services stay in Created because they wait for a healthy db.
Diagnosis
The db log explains the reason in detail.
docker compose logs --no-log-prefix db | head -9
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 (unused mount/volume)
Causes
pgdata:/var/lib/postgresql/dataVolume on the old path
Up to Postgres 17 /var/lib/postgresql/data was common. From 18 the data lives in a version-specific subfolder, and the image refuses to start with a foreign mount below it.
VOLUME in the imageAn anonymous volume is left behind
For the image's VOLUME path Docker also creates an anonymous volume. It survives a normal removal of the container.
Fix
sed -i '' 's#pgdata:/var/lib/postgresql/data$#pgdata:/var/lib/postgresql#' compose.yaml docker compose rm -sfv db docker compose up -d --wait; echo "Exit: $?" Exit: 0
Don't: Go back to Postgres 17 so the old path “works” again – after the next upgrade your data would sit in the anonymous volume.
Docker Compose hands-on, Day 15: repair ten broken Compose stacks against the clock.
Go to day 15 →
FAQ
Where does the data live from Postgres 18?
Under /var/lib/postgresql/18/docker. If you mount the volume on /var/lib/postgresql, the version-specific folder sits inside it.
What does docker compose rm -sfv do?
-s stops the container, -f skips the prompt, -v also removes the container's anonymous volumes.