Skip to content

Create multiple users and databases #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
phlegx opened this issue May 3, 2016 · 10 comments
Closed

Create multiple users and databases #151

phlegx opened this issue May 3, 2016 · 10 comments

Comments

@phlegx
Copy link

phlegx commented May 3, 2016

Hi!

I was wondering on how to create multiple databases with multiple users? As far as I have seen here, currently only one user that has superuser power and the corresponding database can be created trough POSTGRES_USER

Is creating multiple databases and users currently already possible with the ENV variables? If not, is there another way I can use via docker/docker compose?

@mmarzantowicz
Copy link

I think recommended way is to create new images based on postgres:xxx and add custom scripts there. Here is how I do this:

  • Dockerfile
# vim:set ft=dockerfile:
FROM postgres:latest
MAINTAINER ...

# Custom initialization scripts
COPY ./create_user.sh   /docker-entrypoint-initdb.d/10-create_user.sh
COPY ./create_db.sh     /docker-entrypoint-initdb.d/20-create_db.sh
  • create_user.sh
#!/bin/bash
set -e

POSTGRES="psql --username ${POSTGRES_USER}"

echo "Creating database role: ${DB_USER}"

$POSTGRES <<-EOSQL
CREATE USER ${DB_USER} WITH CREATEDB PASSWORD '${DB_PASS}';
EOSQL
  • create_db.sh
#!/bin/bash
set -e

POSTGRES="psql --username ${POSTGRES_USER}"

echo "Creating database: ${DB_NAME}"

$POSTGRES <<EOSQL
CREATE DATABASE ${DB_NAME} OWNER ${DB_USER};
EOSQL

Then I have to run my image with additional environment variables: DB_NAME, DB_USER, DB_PASS. If you want to create more than one db you can go with DB_XX_NAME and so on but it's really up to you how to name variables. Also note, that files files should be executable (+x).

For really massive deployments you can use environment file with docker-compose.

@mrts
Copy link

mrts commented Jul 31, 2017

If you don't want to create a new image, it's also possible to mount the multiple DB creation script directory as a volume, see https://github.com/mrts/docker-postgresql-multiple-databases

@lewispham
Copy link

But still guys, why shouldn't we support this feature in this image?

@MartinKaburu
Copy link

MartinKaburu commented Mar 12, 2019

If you would like to create multiple databases under one user or multiple users, use this script https://github.com/MartinKaburu/docker-postgresql-multiple-databases#using-multiple-databases-with-the-official-postgresql-docker-image

@yami12376
Copy link

Why this is not supported in the main image on docker hub?

@ChrisjanWust
Copy link

ChrisjanWust commented Jan 28, 2020

If you don't want to create a new image, it's also possible to mount the multiple DB creation script directory as a volume, see https://github.com/mrts/docker-postgresql-multiple-databases

If you would like to create multiple databases under one user or multiple users, use this script https://github.com/MartinKaburu/docker-postgresql-multiple-databases#using-multiple-databases-with-the-official-postgresql-docker-image

How would you use this script in conjunction with a docker volume (which maintaining data persistence)? Honestly, it feels like an ugly solution that quickly shows it's hackyness.

@tcurdt
Copy link

tcurdt commented Mar 9, 2020

Is there any chance for this being reopened and improved?

@yosifkit
Copy link
Member

yosifkit commented Mar 9, 2020

Is there any chance for this being reopened and improved?

Short answer, no.
Slightly longer answer: #240 (comment)

@tcurdt
Copy link

tcurdt commented Mar 9, 2020

@yosifkit Could you give an even longer answer, too?

Creating a new image is not great. Mounting a file to /docker-entrypoint-initdb.d/: is better - but again not really ideal. Having everything contained in a single yml is the prefered way with docker compose and friends.

So why is the resistance to improve this? I am honestly just curious if there is a good reason.

@EliYk
Copy link

EliYk commented Jun 6, 2021

If anyone needs this, there seems to be this image -
https://hub.docker.com/r/lmmdock/postgres-multi/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants