Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres and docker-compose : can't create a custom role and database

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 !

like image 512
zlacheman Avatar asked Mar 05 '18 14:03

zlacheman


1 Answers

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.

like image 128
Yuankun Avatar answered Nov 02 '22 08:11

Yuankun