Postgres data vanishes on recreate: volume at the wrong path
Medium

Problem

Every time db is recreated, the database comes up empty — all the data is gone, even though there's a named volume pgdata specifically for persistence and it IS mounted into the container. The volume isn't full, the disk isn't full, and postgres starts fine. Figure out why the data isn't surviving and fix persistence.

Initial setup

  • dbpostgres:15, Up, with the pgdata named volume mounted.
  • Postgres stores its data in /var/lib/postgresql/data.

Example interaction

$ docker inspect -f '{{range .Mounts}}{{.Source}} -> {{.Destination}}{{end}}' db
pgdata -> /data

$ docker logs db | tail -1
WARNING:  /data is mounted but empty — is this the intended data directory?

Acceptance

You've solved it when:

  • You've established the volume is mounted at the wrong path (/data)
while postgres actually writes to /var/lib/postgresql/data — so the real data lands on the container's ephemeral layer and is lost on recreate. The volume isn't broken; it's mounted in the wrong place.
  • You've recreated db running with pgdata mounted at
/var/lib/postgresql/data, so data now lives on the volume.

Constraints

  • Tools: docker CLI only.
  • End state: db running with pgdata mounted at
/var/lib/postgresql/data.

Follow-up

  1. Why does mounting the volume at /data "work" (no error) yet lose data —
where do postgres's writes actually go?
  1. How would you have caught this before losing data (inspecting the mount
destination vs the image's VOLUME/PGDATA)?
  1. Why can't you fix the mount path on a running container without recreate?
Live session
Code
SavedNo commands yet