The Postgres container refuses to start: data directory has invalid permissions
FATAL: data directory "/var/lib/postgresql/data/pgdata" has invalid permissionsShort answer
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. A mounted host folder that is too open, owned by another user, or on a share that ignores chmod breaks this. Fix the mode and owner, or switch to a named volume.
Fix it fast
1Find out which cause you have
Read the numeric owner and the mode of the host folder, and compare them with the user the container runs the server as.
stat -c '%a %u:%g %n' ./pgdata docker run --rm postgres:17 id postgres2Match what you see, then jump to its fix
- 1
The first command shows a mode such as 755, 775 or 777, and the log starts with FATAL about invalid permissions.The folder is more open than 0700Fix - 2
The owner id in the first command differs from the id printed for postgres, or mkdir fails because the parent folder is root-owned.The folder belongs to a user other than the Postgres userFix - 3
chmod 0700 succeeds but stat still shows the old mode.The mounted folder cannot keep Unix modes (Docker Desktop, shared drives)Fix
- 1
What does the error look like?
FATAL: data directory "/var/lib/postgresql/data/pgdata" has invalid permissionsmkdir: cannot create directory ‘/home/postgres/pgdata/data’: permission denied
Cause 1 of 3
The folder is more open than 0700
1Check
PostgreSQL accepts 0700 on directories (0600 on files), or 0750 and 0640 when group read access was enabled at initdb time. Anything wider is refused, and this happens often after a recursive chmod 777 meant to silence a permission error.
stat -c '%a %n' ./pgdata2Fix
Stop the stack, close the folder to everyone but the owner, and start again.
docker compose down
sudo chmod 0700 ./pgdata
docker compose up -d3Verify
The container stays up and the log reaches the line saying the system is ready to accept connections.
docker compose logs db | tail -n 5Confirmed by postgresql.org
Cause 2 of 3
The folder belongs to a user other than the Postgres user
1Check
The data directory belongs to the Postgres user, and it is recommended that this user also owns the parent directory. A folder created by Docker as root, or by your own login, does not meet that.
ls -ldn ./pgdata ./pgdata/..
docker run --rm postgres:17 id -u postgres2Fix
Give the folder to the numeric id the container runs the server as, then set the mode.
docker compose down
sudo chown -R "$(docker run --rm postgres:17 id -u postgres)":"$(docker run --rm postgres:17 id -g postgres)" ./pgdata
sudo chmod 0700 ./pgdata
docker compose up -d3Verify
ls shows the same numeric owner as the postgres user, and the container starts.
ls -ldn ./pgdata
docker compose psConfirmed by postgresql.org
Cause 3 of 3
The mounted folder cannot keep Unix modes (Docker Desktop, shared drives)
1Check
Docker Desktop runs containers in a Linux VM and shares host paths into it, so what you see on a shared path depends on that sharing layer rather than on a normal Linux disk.
chmod 0700 ./pgdata
stat -c '%a %n' ./pgdata2Fix
Stop using a host folder for the database files. Let Docker manage a named volume, which the Docker Engine creates and owns.
services:
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: example-password
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:3Verify
Recreate the stack; the volume is listed and the server comes up.
docker compose up -d
docker volume lsConfirmed by docs.docker.com
How do I stop it coming back?
- Keep database files in a named volume unless you must read them from the host.
- Never fix a permission error on a data directory with chmod -R 777; use 0700 and the right owner.
- When you do bind-mount, create the folder once and set owner and mode before the first start.
- On Postgres 18 and newer, check the path in the image notes before mounting, because the default data path changed.
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 set permissions on a bind-mounted data folder
chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
Postgres needs its data directory owned by the server user with mode 0700.
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
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 fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fix