The Postgres container cannot set permissions on a bind-mounted data folder
initdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permittedShort answer
Postgres needs its data directory owned by the server user with mode 0700. A bind mount from a Windows or macOS folder, or a Linux folder owned by another user, may not allow that. Use a Docker named volume, chown the host folder, or make sure a custom --user exists in the container.
Fix it fast
1Find out which cause you have
Find out who the server runs as and who owns the mounted folder. The two must match.
docker run --rm postgres:17 id postgres ls -ldn ./pgdata2Match what you see, then jump to its fix
- 1
Operation not permitted on chmod while the folder is under your home directory or a Windows drive.A bind mount from a Windows or macOS folder cannot hold Linux permissionsFix - 2
Permission denied, and the numeric owner from the first step differs from the postgres user id.A Linux host folder is owned by a different userFix - 3
initdb: could not look up effective user IDA custom user has no entry in /etc/passwdFix
- 1
What does the error look like?
initdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permittedchmod: changing permissions of '/var/lib/postgresql/data': Operation not permittedinitdb: error: could not look up effective user ID 1000: user does not exist
Cause 1 of 3
A bind mount from a Windows or macOS folder cannot hold Linux permissions
1Check
Docker Desktop runs containers in a Linux VM and shares host folders into it. The database directory must be 0700 and owned by the server user, which that sharing may not allow.
grep -n -B2 -A2 'pgdata' compose.yaml2Fix
Let Docker manage the storage with a named volume instead of a host folder.
services:
db:
image: postgres:17
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:3Verify
Recreate the container; initialisation completes.
docker compose up -d && docker compose logs db | tail -n 5Confirmed by docs.docker.com
Cause 2 of 3
A Linux host folder is owned by a different user
1Check
On Linux a bind mount keeps the host's owner. The server user inside the container cannot write to a folder owned by someone else.
docker run --rm postgres:17 id postgres
ls -ldn ./pgdata2Fix
Give the folder to that numeric id and restrict it to the owner.
sudo chown -R 999:999 ./pgdata # use the uid printed by the id command
sudo chmod 700 ./pgdata3Verify
Start the container and confirm the cluster is created.
docker compose up -d && docker compose logs db | tail -n 5Confirmed by postgresql.org
Cause 3 of 3
A custom user has no entry in /etc/passwd
1Check
The image runs as an arbitrary user set with user: or --user, but initdb requires that user id to exist in /etc/passwd inside the container.
grep -n 'user:' compose.yaml2Fix
Use the image's own postgres user, or run as a user id that exists inside the container.
services:
db:
image: postgres:17
# remove "user: 1000:1000", or mount a passwd file containing that uid3Verify
The container initialises without the lookup error.
docker compose up -d && docker compose logs db | tail -n 5Confirmed by hub.docker.com
How do I stop it coming back?
- Use named volumes for databases; keep bind mounts for source code and config.
- Do not run chmod 777 on a data directory; Postgres rejects loose permissions.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Databases
The Postgres container cannot initialise because its data directory already has files
initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
initdb refuses to run in a directory that already has anything in it.
3 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
A Postgres query in Docker fails with: could not resize shared memory segment
ERROR: could not resize shared memory segment: No space left on device
Postgres in a container hands query memory to parallel workers through the container's /dev/shm.
2 causes, each with a fixDatabases
The Postgres container refuses to start: data directory has invalid permissions
mkdir: cannot create directory ‘/home/postgres/pgdata/data’: permission denied
Postgres starts only when its data directory is owned by the server user and closed to everyone else: mode 0700, or 0750 when group read is enabled.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
manage.py test stops with 'permission denied to create database' on PostgreSQL
django.db.utils.ProgrammingError: permission denied to create database
Django creates a separate database called test_ plus your database name, using the DATABASES USER.
2 causes, each with a fix