Postgres: windows 10 FATAL: data directory "/var/lib/postgresql/data" has wrong ownership

Created on 12 Mar 2019  Â·  6Comments  Â·  Source: docker-library/postgres

On Windows 10 is always failed while docker-compose up:

version: "3"
services:
  db:
    image: postgres:11.2
    environment:
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=test
      - POSTGRES_DB=test
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
    ports:
      - "5433:5432"

and here is logs

db_1  | running bootstrap script ... 2019-03-12 15:05:29.271 UTC [77] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
db_1  | 2019-03-12 15:05:29.271 UTC [77] HINT:  The server must be started by the user that owns the data directory.
db_1  | child process exited with exit code 1
db_1  | initdb: removing contents of data directory "/var/lib/postgresql/data"
noorand_db_1 exited with code 1
question

Most helpful comment

HINT: The server must be started by the user that owns the data directory.

From the perspective of the VM that runs Docker for Windows, any directory shared from the host is owned by root ( https://github.com/docker/for-win/issues/63). If I remember correctly, it allows any user access to read and write anyway. Unfortunately, postgres must be run as the owner of the directory (regardless of whether reads and writes would have succeeded) and will not run as root. So the only solution is to use a named volume rather than a share to the Windows host:

    volumes:
      - postgres-data:/var/lib/postgresql/data
# this name will need to be unique among all your containers

All 6 comments

FATAL: data directory "/var/lib/postgresql/data" has wrong ownership

The folder you're mounting has insufficient permissions/ownership in the container

@wglambert Thank for response, but how can i fix it?

HINT: The server must be started by the user that owns the data directory.

From the perspective of the VM that runs Docker for Windows, any directory shared from the host is owned by root ( https://github.com/docker/for-win/issues/63). If I remember correctly, it allows any user access to read and write anyway. Unfortunately, postgres must be run as the owner of the directory (regardless of whether reads and writes would have succeeded) and will not run as root. So the only solution is to use a named volume rather than a share to the Windows host:

    volumes:
      - postgres-data:/var/lib/postgresql/data
# this name will need to be unique among all your containers

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: lnmp-postgres
spec:
serviceName: lnmp-deploy
replicas: 1
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:11.2-alpine
ports:
- containerPort: 5432
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-data
env:
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
- name: POSTGRES_PASSWORD
value: a.123456
volumes:
- name: postgres-data
persistentVolumeClaim:
claimName: postgres-data
volumeClaimTemplates:
- metadata:
name: postgres-dataI have the same problem。

as that file of docker-compose, write the kube yaml file, then I get this return:

2019-03-22 08:27:21.604 UTC [47] FATAL: data directory "/var/lib/postgresql/data/pgdata" has wrong ownership

so, It's no work for kube in docker-desktop

Unfortunately, postgres must be run as the owner of the directory (regardless of whether reads and writes would have succeeded) and will not run as root.

It seems to be a little bit more than that: I suppose the files are involved too.

/var/lib/docker/volumes # ls -la
total 44
drwx------  5 root     root  4096 Mar 27 14:12 .
drwx--x--x 15 root     root  4096 Mar 26 09:29 ..
drwxr-xr-x  3 root     root  4096 Mar 27 13:57 d708a64dd2273d1c1d1bb1e01477452bdd2dfe2832e8b9c7a3097fd4bb856d6b
drwxr-xr-x  2 root     root  4096 Mar 27 13:54 windows
/var/lib/docker/volumes # ls -la d708a64dd2273d1c1d1bb1e01477452bdd2dfe2832e8b9c7a3097fd4bb856d6b/
total 12
drwxr-xr-x  3 root     root     4096 Mar 27 13:57 .
drwx------  5 root     root     4096 Mar 27 14:12 ..
drwx------ 19 postgres postgres 4096 Mar 27 13:58 _data
/var/lib/docker/volumes # ls -la d708a64dd2273d1c1d1bb1e01477452bdd2dfe2832e8b9c7a3097fd4bb856d6b/_data/
total 124
drwx------ 19 postgres postgres  4096 Mar 27 13:58 .
drwxr-xr-x  3 root     root      4096 Mar 27 13:57 ..
-rw-------  1 postgres postgres     3 Mar 27 13:57 PG_VERSION
drwx------  5 postgres postgres  4096 Mar 27 13:57 base
drwx------  2 postgres postgres  4096 Mar 27 13:57 global
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_commit_ts
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_dynshmem
-rw-------  1 postgres postgres  4537 Mar 27 13:57 pg_hba.conf
-rw-------  1 postgres postgres  1636 Mar 27 13:57 pg_ident.conf
drwx------  4 postgres postgres  4096 Mar 27 13:58 pg_logical
drwx------  4 postgres postgres  4096 Mar 27 13:57 pg_multixact
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_notify
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_replslot
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_serial
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_snapshots
drwx------  2 postgres postgres  4096 Mar 27 13:58 pg_stat
drwx------  2 postgres postgres  4096 Mar 27 13:58 pg_stat_tmp
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_subtrans
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_tblspc
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_twophase
drwx------  3 postgres postgres  4096 Mar 27 13:57 pg_wal
drwx------  2 postgres postgres  4096 Mar 27 13:57 pg_xact
-rw-------  1 postgres postgres    88 Mar 27 13:57 postgresql.auto.conf
-rw-------  1 postgres postgres 23817 Mar 27 13:57 postgresql.conf
-rw-------  1 postgres postgres    24 Mar 27 13:57 postmaster.opts
/var/lib/docker/volumes # ls -la windows
total 8
drwxr-xr-x 2 root     root     4096 Mar 27 13:54 .
drwx------ 5 root     root     4096 Mar 27 14:12 ..
lrwxrwxrwx 1 postgres postgres   27 Mar  8 02:22 _data -> /host_mnt/d/Docker/postgres
/var/lib/docker/volumes # ls -la windows/_data/
total 42
drwxrwxrwx 2 root root  4096 Mar 27 14:36 .
drwxrwxrwx 2 root root     0 Mar 15 13:53 ..
-rwxr-xr-x 1 root root     3 Mar 27 14:14 PG_VERSION
drwxrwxrwx 2 root root     0 Mar 27 14:14 base
drwxrwxrwx 2 root root     0 Mar 27 14:14 global
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_commit_ts
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_dynshmem
-rwxr-xr-x 1 root root  4537 Mar 27 14:14 pg_hba.conf
-rwxr-xr-x 1 root root  1636 Mar 27 14:14 pg_ident.conf
drwxrwxrwx 2 root root     0 Mar 27 14:19 pg_logical
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_multixact
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_notify
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_replslot
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_serial
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_snapshots
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_stat
drwxrwxrwx 2 root root     0 Mar 27 14:35 pg_stat_tmp
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_subtrans
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_tblspc
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_twophase
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_wal
drwxrwxrwx 2 root root     0 Mar 27 14:14 pg_xact
-rwxr-xr-x 1 root root    88 Mar 27 14:14 postgresql.auto.conf
-rwxr-xr-x 1 root root 23817 Mar 27 14:14 postgresql.conf
-rwxr-xr-x 1 root root    24 Mar 27 14:14 postmaster.opts
-rwxr-xr-x 1 root root    94 Mar 27 14:14 postmaster.pid
/var/lib/docker/volumes #

Here I tried to fake the data volume with a symlink, but no luck.

Closing, since this appears to be resolved.

In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

Was this page helpful?
0 / 5 - 0 ratings