Compare commits

..

No commits in common. "91ebbe004c5302626d27e64aecc2182e03a9d57e" and "84385cbf2ae59e6c4f1823ddb0f2c915a251360b" have entirely different histories.

3 changed files with 13 additions and 19 deletions

View File

@ -1,2 +1,2 @@
FROM postgres:14.4 FROM postgres:9.6
COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/

View File

@ -1,16 +1,14 @@
# Using multiple databases with the official PostgreSQL Docker image # Using multiple databases with the official PostgreSQL Docker image
This fork merges [heliocastro:user_pass_improvements](https://github.com/heliocastro/docker-postgresql-multiple-databases/tree/user_pass_improvement) with some slight tweaks to keep the user name the same as the database name.
The [official recommendation](https://hub.docker.com/_/postgres/) for creating The [official recommendation](https://hub.docker.com/_/postgres/) for creating
multiple databases is as follows: multiple databases is as follows:
> If you would like to do additional initialization in an image derived from *If you would like to do additional initialization in an image derived from
> this one, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts under this one, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts under
> `/docker-entrypoint-initdb.d` (creating the directory if necessary). After the `/docker-entrypoint-initdb.d` (creating the directory if necessary). After the
> entrypoint calls `initdb` to create the default `postgres` user and database, entrypoint calls `initdb` to create the default `postgres` user and database,
> it will run any `*.sql` files and source any `*.sh` scripts found in that it will run any `*.sql` files and source any `*.sh` scripts found in that
> directory to do further initialization before starting the service. directory to do further initialization before starting the service.*
This directory contains a script to create multiple databases using that This directory contains a script to create multiple databases using that
mechanism. mechanism.
@ -20,7 +18,8 @@ mechanism.
### By mounting a volume ### By mounting a volume
Clone the repository, mount its directory as a volume into Clone the repository, mount its directory as a volume into
`/docker-entrypoint-initdb.d` and declare database names separated by commas and each entry with database, user and password separated by double colon in `POSTGRES_MULTIPLE_DATABASES` environment variable as follows `/docker-entrypoint-initdb.d` and declare database names separated by commas in
`POSTGRES_MULTIPLE_DATABASES` environment variable as follows
(`docker-compose` syntax): (`docker-compose` syntax):
myapp-postgresql: myapp-postgresql:
@ -28,10 +27,9 @@ Clone the repository, mount its directory as a volume into
volumes: volumes:
- ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d
environment: environment:
- POSTGRES_MULTIPLE_DATABASES=db1:pwd1,db2:pwd2 - POSTGRES_MULTIPLE_DATABASES=db1,db2
- POSTGRES_USER=myapp - POSTGRES_USER=myapp
- POSTGRES_PASSWORD= - POSTGRES_PASSWORD=
- POSTGRES_DB=db
### By building a custom image ### By building a custom image
@ -47,14 +45,13 @@ to the container:
myapp-postgresql: myapp-postgresql:
image: eu.gcr.io/your-project/postgres-multi-db image: eu.gcr.io/your-project/postgres-multi-db
environment: environment:
- POSTGRES_MULTIPLE_DATABASES=db1:pwd1,db2:pwd2 - POSTGRES_MULTIPLE_DATABASES=db1,db2
- POSTGRES_USER=myapp - POSTGRES_USER=myapp
- POSTGRES_PASSWORD= - POSTGRES_PASSWORD=
- POSTGRES_DB=db
### Non-standard database names ### Non-standard database names
If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`: If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`:
environment: environment:
- POSTGRES_MULTIPLE_DATABASES="test-db-1:pwd1","test-db-2:pwd2" - POSTGRES_MULTIPLE_DATABASES="test-db-1","test-db-2"

View File

@ -4,13 +4,10 @@ set -e
set -u set -u
function create_user_and_database() { function create_user_and_database() {
local dbinfo=$1 local database=$1
IFS=":" read -r database password <<< "$dbinfo"
echo " Creating user and database '$database'" echo " Creating user and database '$database'"
echo "Creating database '$database' with user '$user' and password '$password'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "postgres" <<-EOSQL psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "postgres" <<-EOSQL
SELECT 'CREATE USER ' || LOWER(TRIM('$database')) AS create_user_query WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = LOWER(TRIM('$database')));\gexec SELECT 'CREATE USER ' || LOWER(TRIM('$database')) AS create_user_query WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = LOWER(TRIM('$database')));\gexec
ALTER USER $database WITH ENCRYPTED PASSWORD '$password';
SELECT 'CREATE DATABASE ' || LOWER(TRIM('$database')) || ' WITH OWNER "$POSTGRES_USER" ENCODING "UTF8" LC_COLLATE = "en_US.UTF-8" LC_CTYPE = "en_US.UTF-8" TEMPLATE="template0"' AS create_table_query WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = LOWER(TRIM('$database')));\gexec SELECT 'CREATE DATABASE ' || LOWER(TRIM('$database')) || ' WITH OWNER "$POSTGRES_USER" ENCODING "UTF8" LC_COLLATE = "en_US.UTF-8" LC_CTYPE = "en_US.UTF-8" TEMPLATE="template0"' AS create_table_query WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = LOWER(TRIM('$database')));\gexec
GRANT ALL PRIVILEGES ON DATABASE $database TO $database; GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL EOSQL