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

Expected behavior

Persist Postgres data in my host so data won’t be deleted every time I run a container.

Actual behavior

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.

Information

Not sure if it will have any valuable info, but a diagnostic was uploaded with id: BD1854A3-041C-4C22-9FE5-1E5045910B45/2016-07-13_07-36-45

docker-compose.yml

version: '2'

services:
  web:
    build: 
      context: .
      dockerfile: .docker/rails.dockerfile
      args:
        - http_proxy
        - https_proxy
        - no_proxy
    command: bash -c 'rm -f tmp/pids/server.pid && bundle install && bundle exec rails s -b "0.0.0.0"'
    volumes: 
      - d:/test:/var/www
    ports: 
      - "3000:3000"
    depends_on:
      - 'db'
    networks:
      - klipbox-network
      

  db:
    image: postgres
    networks:
      - test-network
    volumes:
      - d:/test/pgpersist:/var/lib/postgresql/data/pgdata
    ports:
        - "5432:5432"
    environment:
      PGDATA: /var/lib/postgresql/data/pgdata


networks:
  test-network:
    driver: bridge

Don’t know if it makes any difference, but the d:\test directory is a Git repository, however pgpersist directory has been added to .gitignore

Steps to reproduce the behavior

  1. …Use the docker-compose file above
  2. …Run docker-compose up -d
1 Like

Same here :frowning:

I tried everything and still cannot start Postgres with mounted volume. The funny thing is that previous build (RC2) worked ok.

Using a volume for your postgresql data should do the trick.

Sadly, all the mounts are made as root, thus any data mounted directly will belong to the root user.

I don’t get what you’ve said @greut. Isn’t it what I’m doing in my docker-compose.yml?

I have the same problem. I tried mounting a data volume, but no success. Here is my docker-compose file:

version: '2'
services:
  db:
    image: postgres
    volumes_from:
      - postgres_data
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  postgres_data:
    image: busybox
    volumes:
      - ./postgres:/var/lib/postgresql/data

This problem is still present in beta19.
By the way, it works if I create a volume without mounting a host directory like:

services:
  db:
    image: postgres
    volumes:
      - /var/lib/postgresql/data
3 Likes

That’s probably because in this case the volume is created in a path owned by Docker (I think).

This is not a problem if used with Docker Toolbox by the way.

I am also experiencing this issue when attempting to run Postgres within a Docker container. I did a bit of digging and it appears that the Docker for Windows client (1.12.0-rc4-beta19) is mounting the volumes using CIFS and is passing a slew of options:

//10.0.75.1/D
/tmp/wtf
cifs
rw,
relatime,
vers=1.0,
cache=strict,
username=user,
domain=WORKSTATION,
uid=0,
noforceuid,
gid=0,
noforcegid,
addr=10.0.75.1,
file_mode=0755,
dir_mode=0755,
iocharset=utf8,
nounix,
serverino,
mapposix,
noperm,
rsize=61440,
wsize=65536,
actimeo=1
0 0

Of particular interest are uid, gid, noforceuid, noforcegid, and nounix. I haven’t poked around at this yet but it seems as if we are getting stuck with uid=0,gid=0 which corresponds to root:root (as @greut pointed out).

In my experience mapping uid/gid between Windows and Linux can be pretty painful due to differences between the POSIX and NTFS ACL schemes. One potential solution could be more granular management of shares and mount options. I would be very interested to hear others’ opinions on this issue and any potential solutions.

1 Like

what you’re doing is a local volume (which will belong to root), use a docker data volume. What you’re doing is a host directory as a volume. https://docs.docker.com/engine/tutorials/dockervolumes/

But how do I create a data volume mapped to a local volume on Docker Compose? I need to persist the database server info so I don’t lose the information each time I run the container.

version: '2'
services:
  db:
     image: postgres:9.4
     volumes:
        - postgres:/var/lib/postgresql/data
volumes:
  postgres:   

But in this case it will create the local volume on Docker folder, right? If so, can’t I map to a different place?

Try another plugin then: https://docs.docker.com/engine/extend/plugins/#/volume-plugins

1 Like

Hello

Same problem with new version 1.12.0-beta21 (build: 5971) 5b59d37

When I don’t map volume like this:

  • /var/lib/postgresql/data
    it works

But when I map a volume like this:

  • .data/postgresql:/var/lib/postgresql/data
    it doesn’t work

It is frustrating because each time I update docker for windows I loose data. I need to backup before update and restore my database after update.

Issue is valid even in Stable version. :frowning: Anybody any idea how to workaround this?

Yep same issue here. I’ve tried doing it in my User directory on my C: drive instead but ends up with:

db_1     | syncing data to disk ... initdb: could not fsync file "/var/lib/postgresql/data/base/1": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/base/12374": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/base/12379": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/base": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/global": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_clog": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_commit_ts": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_dynshmem": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_logical/mappings": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_logical/snapshots": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_logical": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_multixact/members": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_multixact/offsets": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_multixact": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_notify": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_replslot": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_serial": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_snapshots": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_stat": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_stat_tmp": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_subtrans": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_tblspc": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_twophase": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_xlog/archive_status": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_xlog": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data": Invalid argument
db_1     | initdb: could not fsync file "/var/lib/postgresql/data/pg_tblspc": Invalid argument

Then

db_1     | waiting for server to start....FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
db_1     | HINT:  The server must be started by the user that owns the data directory.

It does seem to create files and folders but just won’t start based on permissions

Are there any workarounds?

Are there any workarounds?

Workaround: add the environment variable PGDATA: /tmp to your docker-compose file.

Here’s mine:

version: '2'

services:
  postgres:
    image: postgres:latest
    #tty: true
    ports:
      - "5432:5432"
    volumes:
      - f:/data/docker/postgresql/data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: docker
      POSTGRES_DB: db
      PGDATA: /tmp
5 Likes