Permission issue with PostgreSQL in docker container

The other answer indeed points to the root cause of the problem, however the help page it points to does not contain a solution. Here is what I came up with to make this work for me:

  1. start the container using your normal docker-compose file, this creates the directory with the hardcoded uid:gid (999:999)
version: '3.7'

services:
  db:
    image: postgres
    container_name: postgres
    volumes:
      - ./data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: fake_database_user
      POSTGRES_PASSWORD: fake_database_PASSWORD
  1. stop the container and manually change the ownership to uid:gid you want (I’ll use 1000:1000 for this example
$ docker stop postgres
$ sudo chown -R 1000:1000 ./data 
  1. Edit your docker file to add your desired uid:gid and start it up again using docker-compose (notice the user:)
version: '3.7'

services:
  db:
    image: postgres
    container_name: postgres
    volumes:
      - ./data:/var/lib/postgresql/data
    user: 1000:1000
    environment:
      POSTGRES_USER: fake_database_user
      POSTGRES_PASSWORD: fake_database_password

The reason you can’t just use user: from the start is that if the image runs as a different user it fails to create the data files.

On the image documentation page, it does mention a solution to add a volume to expose the /etc/passwd file as read-only in the image when providing --user option, however, that did not work for me with the latest image, as I was getting the following error. In fact none of the three proposed solutions worked for me.

initdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permitted

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)