Data directory "/var/lib/postgresql/data/pgdata" has wrong ownership

Hi Guys,

I was working on docker linux RHEL 8.2 and was facing the above mentioned issue.
I was working on Docker CE - 23.0.1 and with postgres image : postgres:15.2-alpine3.17

I was able to setup Docker on my local system by giving a mountpoint that was other than default docker directory which is /postgresdata
Here what is happening is in postgres container that gets spinned up the user postgres has uid :70, so when you try to give it an external mountpoint it is updating the owner of that directory to postgres, luckily on my filesystem there was a user named avasti that had uid as 70.
Hence, I was able to start postgres on my local filesystem but when I wanted to give my mountpoint as an NFS mountpoint something like /cluster/postgresdata which is shared across two servers. It used to fail giving me below warnings :

db_1 | FATAL: data directory “/var/lib/postgresql/data/pgdata” has wrong ownership
db_1 | HINT: The server must be started by the user that owns the data directory.

This is because the uid 70 did not have access to my NFS shared drive - /cluster/postgresdata

However on deeply investigating the issue, I tried to contact my Infra Team and requested them to create a new user with uid 70 and give it permission to access NFS Shared directory, but they did not agree as uid 70 was already taken by other user on system.

In order to resolve this issue, i mounted the /etc/passwd file in the docker container by changing the uid of postgres user to the UID of the user on my filesystem that has access to NFS Shared drive - /cluster/postgresdata and voila it worked for me.

Below is the docker-compose.yml file that I used :

version: ‘3.3’
services:
bw6-postgresdata:
image: postgres:15.2-alpine3.17
ports:
- “8422:5432”
deploy:
restart_policy:
condition: on-failure
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: $trongPassw0rd
PGDATA : /var/lib/postgresql/data/pgdata
volumes:
- /cluster/postgresdata:/var/lib/postgresql/data/pgdata
- /cluster/passwd:/etc/passwd
networks:
- pgdb_network

networks:
pgdb_network:
external: true

Here, in my case the user I want to use has uid 1017, so replacing the postgres UID with 1017, you all will have to use the uid of the user that is available on your filesystem and has access to the directory which you want to use as mountpoint for storing your data created in the container, so that you don’t loose data in event of container crash or node crash.

I updated the same in the passwd file and mapped it with container’s /etc/passwd file:

root:x:0:0:root:/root:/bin/ash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/mail:/sbin/nologin
news:x:9:13:news:/usr/lib/news:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucppublic:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
man:x:13:15:man:/usr/man:/sbin/nologin
postmaster:x:14:12:postmaster:/var/mail:/sbin/nologin
cron:x:16:16:cron:/var/spool/cron:/sbin/nologin
ftp:x:21:21::/var/lib/ftp:/sbin/nologin
sshd:x:22:22:sshd:/dev/null:/sbin/nologin
at:x:25:25:at:/var/spool/cron/atjobs:/sbin/nologin
squid:x:31:31:Squid:/var/cache/squid:/sbin/nologin
xfs:x:33:33:X Font Server:/etc/X11/fs:/sbin/nologin
games:x:35:35:games:/usr/games:/sbin/nologin
cyrus:x:85:12::/usr/cyrus:/sbin/nologin
vpopmail:x:89:89::/var/vpopmail:/sbin/nologin
ntp:x:123:123:NTP:/var/empty:/sbin/nologin
smmsp:x:209:209:smmsp:/var/spool/mqueue:/sbin/nologin
guest:x:405:100:guest:/dev/null:/sbin/nologin
nobody:x:65534:65534:nobody:/:/sbin/nologin
postgres:x:1017:1017:Linux User,:/var/lib/postgresql:/bin/sh

Hope this works for your as well on linux.
Some windows experts can try this on windows as well, to see if it resolves their issue.
Thank you all for your valuable inputs above, it helped me to investigate in the right direction and try to find a workaround to resolve this issue.

Apparently you are not aware of --user (docker run) or user: (compose file), which allows to set the uid or uid:gid used to run the container.

The official postgres image description has the section “Arbitrary --user Notes”, which covers how it’s intended to be done.

Hope you didn’t spend too much time of tackling it on your own.
.

Thanks @meyay, for the knowledge, appreciate it.

Initially I did try with user property, but somehow that did not work for me.

Hence, had to go forward by mounting the contents to /etc/passwd file in the container, which eventually resolved the issue for me.

perfect, it was my problem, I created a user that not existed in the file system, and the database was not up