20

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 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ! but I don't undestand on lot of documentation they use the notation "ENV_VAR: value" for example : hub.docker.com/_/postgres or docs.docker.com/samples/library/postgres/…
I had to remove the docker volume for postgres github.com/docker-library/postgres/issues/…
You either use - VAR=value or VAR: value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.