Compare commits
6 Commits
84385cbf2a
...
91ebbe004c
Author | SHA1 | Date | |
---|---|---|---|
|
91ebbe004c | ||
|
fec53d8488 | ||
|
2c047bab97 | ||
|
4f18650f85 | ||
|
171052da65 | ||
|
6151b43cba |
@ -1,2 +1,2 @@
|
||||
FROM postgres:9.6
|
||||
FROM postgres:14.4
|
||||
COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/
|
||||
|
25
README.md
25
README.md
@ -1,14 +1,16 @@
|
||||
# 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
|
||||
multiple databases is as follows:
|
||||
|
||||
*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
|
||||
`/docker-entrypoint-initdb.d` (creating the directory if necessary). After the
|
||||
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
|
||||
directory to do further initialization before starting the service.*
|
||||
> 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
|
||||
> `/docker-entrypoint-initdb.d` (creating the directory if necessary). After the
|
||||
> 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
|
||||
> directory to do further initialization before starting the service.
|
||||
|
||||
This directory contains a script to create multiple databases using that
|
||||
mechanism.
|
||||
@ -18,8 +20,7 @@ mechanism.
|
||||
### By mounting a volume
|
||||
|
||||
Clone the repository, mount its directory as a volume into
|
||||
`/docker-entrypoint-initdb.d` and declare database names separated by commas in
|
||||
`POSTGRES_MULTIPLE_DATABASES` environment variable as follows
|
||||
`/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-compose` syntax):
|
||||
|
||||
myapp-postgresql:
|
||||
@ -27,9 +28,10 @@ Clone the repository, mount its directory as a volume into
|
||||
volumes:
|
||||
- ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d
|
||||
environment:
|
||||
- POSTGRES_MULTIPLE_DATABASES=db1,db2
|
||||
- POSTGRES_MULTIPLE_DATABASES=db1:pwd1,db2:pwd2
|
||||
- POSTGRES_USER=myapp
|
||||
- POSTGRES_PASSWORD=
|
||||
- POSTGRES_DB=db
|
||||
|
||||
### By building a custom image
|
||||
|
||||
@ -45,13 +47,14 @@ to the container:
|
||||
myapp-postgresql:
|
||||
image: eu.gcr.io/your-project/postgres-multi-db
|
||||
environment:
|
||||
- POSTGRES_MULTIPLE_DATABASES=db1,db2
|
||||
- POSTGRES_MULTIPLE_DATABASES=db1:pwd1,db2:pwd2
|
||||
- POSTGRES_USER=myapp
|
||||
- POSTGRES_PASSWORD=
|
||||
- POSTGRES_DB=db
|
||||
|
||||
### Non-standard database names
|
||||
|
||||
If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`:
|
||||
|
||||
environment:
|
||||
- POSTGRES_MULTIPLE_DATABASES="test-db-1","test-db-2"
|
||||
- POSTGRES_MULTIPLE_DATABASES="test-db-1:pwd1","test-db-2:pwd2"
|
||||
|
@ -4,10 +4,13 @@ set -e
|
||||
set -u
|
||||
|
||||
function create_user_and_database() {
|
||||
local database=$1
|
||||
local dbinfo=$1
|
||||
IFS=":" read -r database password <<< "$dbinfo"
|
||||
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
|
||||
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
|
||||
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
|
||||
EOSQL
|
||||
|
Loading…
x
Reference in New Issue
Block a user