sameersbn / docker-postgresql

Dockerfile to build a PostgreSQL container image which can be linked to other containers.
MIT License
1.05k stars 467 forks source link

Using PostgreSQL with multiple users and databases #92

Open lchigami opened 7 years ago

lchigami commented 7 years ago

I'm using this image with Gitlab without flaws, but now I'm trying to add a new service that requires a DB service. Then i saw the issue #11 and tried to use the multiple databases feature. Removed the old PostgreSQL container, added to the command the second database like the example:

docker run -it --rm \ -e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' -e DB_NAME='db1,db2' \ sameersbn/postgresql

And when i tried to run the Gitlab docker (sameersbn/gitlab), the log output showed the error:

psql: FATAL: database "db1,db2" does not exist

Is this a bug? Or am I using it wrong?

avluis commented 7 years ago

I wasn't able to get this working either but I did get a bit further. You need to add the DB_NAME environment variable to the gitlab container (--env 'DB_NAME=gitlabhq_production') and it should resolve the database name.

riuvshin commented 7 years ago

same issue here pg 9.6

r2evans commented 6 years ago

I think the use of DB_USER, DB_PASS, and DB_NAME are solely for setting up the database and users the first time. Assuming you use the same database storage (whether a shared data container or a persistent host volume), then once you have used these parameters once, you can comment them out for subsequent starts. This means if you provide these again, they may overwrite or augment what is already there.

For example, I have a docker-compose.yml file that contains this:

version: '2'

services:
  postgresql:
    image: sameersbn/postgresql:9.6-2
    entrypoint: /sbin/entrypoint.sh -c log_connections=on -c log_min_duration_statement=1000
    volumes:
    - /srv/docker/postgresql:/var/lib/postgresql:Z
    ports:
    - "5432:5432"
    environment:
    - USERMAP_UID=987
    - USERMAP_GID=987
    - DB_EXTENSION=pg_trgm
    ### GITLAB
    - DB_USER=gitlab_user
    - DB_PASS=secretpassword1
    - DB_NAME=gitlab_db

Then once I start it up, I can completely comment out the gitlab set of user/pass/name. Then when I want to add a redmine database, I change my docker-compose.yml file to

version: '2'

services:
  postgresql:
    image: sameersbn/postgresql:9.6-2
    entrypoint: /sbin/entrypoint.sh -c log_connections=on -c log_min_duration_statement=1000
    volumes:
    - /srv/docker/postgresql:/var/lib/postgresql:Z
    ports:
    - "5432:5432"
    environment:
    - USERMAP_UID=987
    - USERMAP_GID=987
    - DB_EXTENSION=pg_trgm
    ### GITLAB
    #- DB_USER=gitlab_user
    #- DB_PASS=secretpassword1
    #- DB_NAME=gitlab_db
    ### REDMINE
    - DB_USER=redmine_user
    - DB_PASS=anothersecretpassword
    - DB_NAME=redmine_prod

then

docker-compose stop postgresql
docker-compose rm postgresql
docker-compose up -d postgresql

At this point, both the gitlab and redmine databases and users are available. (I can then comment out the redmine rows. Keeping one of the two uncommented should not pose a problem for normal operations.)

Two notables about this: