Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
derakhshanfar opened this issue Mar 12, 2019 · 8 comments
Labels
question Usability question, not directly related to an error with the image

Comments

@derakhshanfar
Copy link

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
@wglambert wglambert added the question Usability question, not directly related to an error with the image label Mar 12, 2019
@wglambert
Copy link

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

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

@derakhshanfar
Copy link
Author

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

@yosifkit
Copy link
Member

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 ( docker/for-win#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

@lluck42

This comment has been minimized.

@willemavjc
Copy link

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.

@tianon
Copy link
Member

tianon commented Apr 24, 2019

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.

@fierytortilla
Copy link

fierytortilla commented Feb 2, 2021

Ok, so here is a solution that helped me. First, I ran the command docker volume create --name=pgdata in the terminal. Then my co-worker (who has a lot more experience with Docker than me, who has barely any) helped me modify my docker-compose.yml file so that it included the following:

  services:
      db:
          image: postgres
          restart: unless-stopped
          env_file:
            - .env
          volumes:
            - pgdata:/var/lib/postgresql/data
          ports:
            - "5434:5432"
  volumes:
    pgdata:
      external: true

Note: the volumes block is indented at the same level as services. This apparently helps tell Docker that the particular folder being used for data storage is defined as a volume, which has a different set of access rights versus a folder. I'm not entirely sure if this is the correct way to explain it, but my error of data directory "/var/lib/postgresql/data" has invalid permission has ceased and the app is now working.

@nrgapple
Copy link

nrgapple commented Apr 9, 2021

@fierytortilla where are you specifying the local directory that you are mapping from?

volumes:
  - pgdata:/var/lib/postgresql/data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usability question, not directly related to an error with the image
Projects
None yet
Development

No branches or pull requests

8 participants