postgres and docker-compose : can't create a c

2020-07-04 08:09发布

问题:

I'am trying to create a simple postgreSQL container with a custum user and database.

This is my docker-compose file :

version: '2'
services:
  db.postgres:
    container_name: db.postgres
    image: postgres:10
    environment:
      - POSTGRES_USER:'myuser'
      - POSTGRES_PASSWORD:'myuserpassword'
      - POSTGRES_DB:'mydb'
    ports:
      - '5432:5432'
    volumes:
      - ./pgdata:/var/lib/postgresql/data

And the error when I try to connect to my database.

docker exec -it db.postgres psql -U myuser myuserpassword
psql: FATAL:  role "myuser" does not exist

OR

$ docker exec -it db.postgres /bin/bash
root@1a0531e0350f:/# psql -U myuser
psql: FATAL:  role "myuser" does not exist

Docker-compose environment variables appear to be ignored when creating the database.

I don't known what can I do. Do you have an idea of ​​the problem?

Thanks !

回答1:

Modify your docker-compose.yml file as below:

version: '2'
services:
  db.postgres:
    container_name: db.postgres
    image: postgres:10
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=myuserpassword
      - POSTGRES_DB=mydb
    ports:
      - '5432:5432'
    volumes:
      - ./pgdata:/var/lib/postgresql/data

Then drop your current data entirely and re-run docker-compose up.