Cannot map postgres volume

I am trying to create a PostgreSQL container with volume mapping:

docker run -d --name postgres11 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -v /Users/codrut/postgresdata:/var/lib/postgresql/data postgres:11.17-bullseye

this fails with:

–auth-local and --auth-host, the next time you run initdb.

waiting for server to start…2022-08-17 19:35:07.325 UTC [49] FATAL: data directory “/var/lib/postgresql/data” has wrong ownership

2022-08-17 19:35:07.325 UTC [49] HINT: The server must be started by the user that owns the data directory.

stopped waiting

pg_ctl: could not start server

Examine the log output.

Quote from the description of the Postgres image:

PGDATA

This optional variable can be used to define another location - like a subdirectory - for the database files. The default is /var/lib/postgresql/data. If the data volume you’re using is a filesystem mountpoint (like with GCE persistent disks) or remote folder that cannot be chowned to the postgres user (like some NFS mounts), Postgres initdb recommends a subdirectory be created to contain the data.

So if you bind mount a folder instead of using a local volume, you can try to set PGDATA to use a subfolder of “data”. Example in the linked description. Usually setting proper permissions and groups on the source folder is enough, but sometimes services inside the container try to change permissions unconditionally. I don’t remember if Postgres is one of those services.

1 Like

Thank you. Indeed it worked after changing postgresdata with data