Docker container shuts down giving 'data directory has wrong ownership' error when executed in windows 10

This is a documented problem with the Postgres Docker image on Windows [1][2][3][4]. Currently, there doesn't appear to be a way to correctly mount Windows directories as volumes. You could instead use a persistent Docker volume, for example:

  db:
    image: postgres
    environment:
      - POSTGRES_USER=attendize
      - POSTGRES_PASSWORD=attendize
      - POSTGRES_DB=attendize
    ports:
      - "5433:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
    - attendizenet

volumes:
  pgdata:

Other things that didn't work:

  • Set PGDATA to a subdirectory (See PGDATA Setting)
    environment:
      - PGDATA=/var/lib/postgresql/data/mnt
    volumes:
      - ./pgdata:/var/lib/postgresql/data
  • Use a Bind Mount (docker-compose 3.2)
    volumes:
      - type: bind
        source: ./pgdata
        target: /var/lib/postgresql/data
  • Running as POSTGRES_USER=root

More Information:

GitHub

Docker Forums

  • postgresql-data-pgdata-has-wrong-ownership
  • postgres-to-work-on-persistent-windows-mount

Please refer reinierkors' answer from here. The answer is as follows copied as is from the link here for reader's convenience and works for me

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