Home > database >  Create a postgres docker-compose with a local mount volume
Create a postgres docker-compose with a local mount volume

Time:02-01

I have the follow code of my docker-compose.yml:

version: '3'
services:
  db:
    image: postgres:14.6
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    volumes:
      - ./data:/var/lib/postgresql/data

But when I do docker compose up, the console give me the next error:

servicios-basesdatos-db-1 exited with code 1
servicios-basesdatos-db-1  | The files belonging to this database system will be owned by user "postgres".
servicios-basesdatos-db-1  | This user must also own the server process.
servicios-basesdatos-db-1  | 
servicios-basesdatos-db-1  | The database cluster will be initialized with locale "en_US.utf8".
servicios-basesdatos-db-1  | The default database encoding has accordingly been set to "UTF8".
servicios-basesdatos-db-1  | The default text search configuration will be set to "english".
servicios-basesdatos-db-1  | 
servicios-basesdatos-db-1  | Data page checksums are disabled.
servicios-basesdatos-db-1  | 
servicios-basesdatos-db-1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
servicios-basesdatos-db-1  | creating subdirectories ... ok
servicios-basesdatos-db-1  | selecting dynamic shared memory implementation ... posix
servicios-basesdatos-db-1  | selecting default max_connections ... 20
servicios-basesdatos-db-1  | selecting default shared_buffers ... 400kB
servicios-basesdatos-db-1  | selecting default time zone ... Etc/UTC
servicios-basesdatos-db-1  | creating configuration files ... ok
servicios-basesdatos-db-1  | 2023-01-31 09:07:54.976 UTC [83] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
servicios-basesdatos-db-1  | 2023-01-31 09:07:54.976 UTC [83] HINT:  The server must be started by the user that owns the data directory.
servicios-basesdatos-db-1  | child process exited with exit code 1
servicios-basesdatos-db-1  | initdb: removing contents of data directory "/var/lib/postgresql/data"

And I can't up my docker.

How can I fix it so that it works?

CodePudding user response:

Think this line is crucial here

servicios-basesdatos-db-1  | 2023-01-31 09:07:54.976 UTC [83] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership

You may try this solution (initialization of volume in volumes) like:

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

CodePudding user response:

If you look at the docker file if the image here https://github.com/docker-library/postgres/blob/41bd7bf3f487e6dc0036fd73efaff6ccb6fbbacd/14/bullseye/Dockerfile you'll see that they create a user with id 999. I guess your local data directory is owned by you, which is in most cases user 1000. So if you chown the data directory to user 999, it should work.

  • Related