docker-library/postgres

Windows 10 is always FATAL: data directory "/var/lib/postgresql/data" has wrong ownership

Closed this issue ยท 12 comments

On Windows 10 Pro is always failed while docker-compose up, otherwise on mac is work perfectly.

Here are some my docker-compose file:

version: '2'
services:
.
.
.
.
  postgresql:
    image: postgres:93
    container_name: lnpp-postgresql
    restart: always
    environment:
      POSTGRES_PASSWORD: asdf
    volumes:
      - ./postgresql/data:/var/lib/postgresql/data
      - ./postgresql/conf:/etc/postgresql/
    ports:
      - "5432:5432"
    networks:
      lnpp-network:
        ipv4_address: ${lnpp_POSTGRES_IP}

networks:
  lnpp-network:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: ${lnpp_SUBNET}
          gateway: ${lnpp_GATEWAY}

and there are some logs.

lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql exited with code 1
lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql exited with code 1
lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql | FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
lnpp-postgresql | HINT:  The server must be started by the user that owns the data directory.
lnpp-postgresql exited with code 1

I have this same issue only on Windows. Everywhere else is fine.

The only way I was able to get around this was through a named volume mount:

postgresql:
  image: postgres:93
  volumes:
  - postgres:/var/lib/postgresql/data
  - ./postgresql/conf:/etc/postgresql/
...
volumes:
  postgres:

Unfortunately, this is likely docker/for-win#445, and there's not really anything we can do to fix it directly in the image. You might be able to work around it by messing with --user (user: in the compose YAML), but there are some caveats to that noted in https://hub.docker.com/_/postgres/.

Are there any updates on this issue? Here is a simple case to produce this error:

Compose file:

version: "3.7"
services:
  postgres-test:
    restart: always
    image: postgres:latest
    ports:
      - "5432:5432"
    environment:
      - PGDATA=/var/lib/postgresql/data/pgdata
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    volumes:
      - ./postgres/data:/var/lib/postgresql/data/pgdata

Docker file:

FROM postgres:11.5-alpine
RUN mkdir -p "$PGDATA" && chmod -R 700 "$PGDATA" && chown -R postgres:postgres "$PGDATA"

I tried 1) changing the location of the data folder 2) running as root and 3) changing the ownership of the data folder. I can't use a named volume as others have suggested because I need to mount the data folder to a specific location on my server.

I have the same issue running:
docker run --name some-postgres -v E:\tmp\postgresBaza:/var/lib/postgresql/data -d postgres:9.6.15

I get from logs:

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

Maybe there is a way to get around this using the userns-remap on the deamon? Must the docker image be "prepared" to use that feature?

I solved this by mapping my local volume one directory below the one Postgres needs:

version: '3'
services:
  postgres:
    image: postgres
    restart: on-failure
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - PGDATA=/var/lib/postgresql/data/pgdata
      - POSTGRES_DB=postgres
    volumes:
      - ./postgres_data:/var/lib/postgresql
    ports:
      - 5432:5432

This solution worked for me:

docker/for-win#445 (comment)

I have same issue with official image on windows 10. even I set up the PGDATA to a lower level subdirectory as :
docker run -d -e POSTGRES_USER=xxxx-e POSTGRES_PASSWORD=xxxx-e POSTGRES_DB=postgres -e PGDATA=/var/lib/postgresql/data/pgdata -p 5433:5432 -v /D/PostgreSQL/DockerData:/var/lib/postgresql/data --name db postgres

I solved this by mapping my local volume one directory below the one Postgres needs:

version: '3'
services:
  postgres:
    image: postgres
    restart: on-failure
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - PGDATA=/var/lib/postgresql/data/pgdata
      - POSTGRES_DB=postgres
    volumes:
      - ./postgres_data:/var/lib/postgresql
    ports:
      - 5432:5432

Thank U!! I solve my problem

sg552 commented

I met this problem when:

  1. I run windows docker using wsl-2 mode
  2. then I re-installed windows docker using 'wsl-1mode with existing files which is generated bywsl-2` model.

switch to wsl-2 solved my problem.

ๅ›พ็‰‡

Any updates on this issue? I'm trying to deploy a Postgresql Docker container in Microsoft Azure. Their YAML file doesn't have the ability to use named volumes as far as I can tell. When I'm using azureFile, which is basically a bind mount, I stumble upon this issue as well...

I solved this by mapping my local volume one directory below the one Postgres needs:

version: '3'
services:
  postgres:
    image: postgres
    restart: on-failure
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - PGDATA=/var/lib/postgresql/data/pgdata
      - POSTGRES_DB=postgres
    volumes:
      - ./postgres_data:/var/lib/postgresql
    ports:
      - 5432:5432

it worked for me. Thank you!