I'm trying to run postgresql. after checking their document, I added this volume mount
/path/to/data:/var/lib/postgresql/18/docker
my container failed immedeately,

chmod: changing permissions of '/var/run/postgresql': Operation not permitted
The files belonging to this database system will be owned by user "postgres".
...
2025-11-29 12:04:40.102 UTC [73] FATAL:  data directory "/var/lib/postgresql/18/docker" has invalid permissions

after checking document, by default it use 999:999 to run this container. which is not existing in my physical machine.
how can I properly set folder /path/to/dataowner?
I believe most of our containers has such an issue, rather than run with root what's the best practice?

3 Replies 3

There's no particular requirement for numeric user IDs to "exist", and you can sudo chmod 999 /path/to/data if you want.

If a container needs access to the host's filesystem, a common practice is to run it as a user or group that owns the file. Adding a group ID is easy

docker run --group-add users -v /path/to/data:/var/lib/postgresql/18/docker ...

You can also specify the user ID. The postgres image has specific requirements around this, and you may need an additional bind mount.

docker run -u myapp -v /etc/passwd:/etc/passwd -v /path/to/data:/var/lib/postgresql/18/docker ...

But probably the easiest thing to do here is to use a Docker named volume, which avoids all of these problems. You won't be able to directly access the files (but you wouldn't be able to do anything useful with them even if you could), including for backups and restores (but pg_dump/pg_restore work fine).

docker volume create pgdata
docker run -v pgdata:/var/lib/postgresql/18/docker ...

how can I properly set folder /path/to/dataowner?

chown 999:999 /path/to/dataowner

The user with that uid does not have to exist, uid can be any integer.

well you can use device flag for eg:docker run --device=/dev/ttyUSB0 or docker run -it -v /dev:/dev --privileged

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.