docker-postgresql icon indicating copy to clipboard operation
docker-postgresql copied to clipboard

Using PostgreSQL with multiple users and databases

Open lchigami opened this issue 9 years ago • 3 comments

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?

lchigami avatar Feb 09 '17 10:02 lchigami

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.

avluis avatar Apr 13 '17 09:04 avluis

same issue here pg 9.6

riuvshin avatar Jul 28 '17 12:07 riuvshin

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:

  • I think the realization is that these three env-vars are a one-time need to set up for each needed database, not to set up each time you restart a server.
  • If you need multiple databases on a new instance, then I see no other way than to start it with the first database setup, then stop the container, and restart it with the next database setup.

r2evans avatar Jul 07 '18 21:07 r2evans