Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

The Postgres container refuses to start: data directory has invalid permissions

ErrorFATAL: data directory "/var/lib/postgresql/data/pgdata" has invalid permissions

Short 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.

  • Applies to postgres Docker image 17, Docker Engine 28, Docker Compose 2, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 postgres
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3chmod 0700 succeeds but stat still shows the old mode.The mounted folder cannot keep Unix modes (Docker Desktop, shared drives)Fix

What does the error look like?

  • FATAL: data directory "/var/lib/postgresql/data/pgdata" has invalid permissions
  • mkdir: 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' ./pgdata

2Fix

Stop the stack, close the folder to everyone but the owner, and start again.

docker compose down
sudo chmod 0700 ./pgdata
docker compose up -d

3Verify

The container stays up and the log reaches the line saying the system is ready to accept connections.

docker compose logs db | tail -n 5

Confirmed by postgresql.org

Did this fix it?

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 postgres

2Fix

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 -d

3Verify

ls shows the same numeric owner as the postgres user, and the container starts.

ls -ldn ./pgdata
docker compose ps

Confirmed by postgresql.org

Did this fix it?

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' ./pgdata

2Fix

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 ls

Confirmed by docs.docker.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.