Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

The Postgres container cannot set permissions on a bind-mounted data folder

Errorinitdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permitted

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

  • Applies to postgres Docker image 17 and 18, Docker Desktop 4, Docker Engine 28, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1Operation 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. 2Permission 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. 3initdb: could not look up effective user IDA custom user has no entry in /etc/passwdFix

What does the error look like?

  • initdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permitted
  • chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
  • initdb: 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.yaml

2Fix

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 5

Confirmed by docs.docker.com

Did this fix it?

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

2Fix

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

3Verify

Start the container and confirm the cluster is created.

docker compose up -d && docker compose logs db | tail -n 5

Confirmed by postgresql.org

Did this fix it?

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

2Fix

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 uid

3Verify

The container initialises without the lookup error.

docker compose up -d && docker compose logs db | tail -n 5

Confirmed by hub.docker.com

Did this fix it?

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

Next step

Want this handled for good?

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