Skip to content

Commit 171052d

Browse files
committed
Merge remote-tracking branch 'heliocastro/user_pass_improvement'
2 parents 84385cb + 6151b43 commit 171052d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ mechanism.
1818
### By mounting a volume
1919

2020
Clone the repository, mount its directory as a volume into
21-
`/docker-entrypoint-initdb.d` and declare database names separated by commas in
22-
`POSTGRES_MULTIPLE_DATABASES` environment variable as follows
21+
`/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
2322
(`docker-compose` syntax):
2423

2524
myapp-postgresql:
2625
image: postgres:9.6.2
2726
volumes:
2827
- ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d
2928
environment:
30-
- POSTGRES_MULTIPLE_DATABASES=db1,db2
29+
- POSTGRES_MULTIPLE_DATABASES=db1:user:pwd,db2:user:pwd
3130
- POSTGRES_USER=myapp
3231
- POSTGRES_PASSWORD=
32+
- POSTGRES_DB=db
3333

3434
### By building a custom image
3535

@@ -48,10 +48,11 @@ to the container:
4848
- POSTGRES_MULTIPLE_DATABASES=db1,db2
4949
- POSTGRES_USER=myapp
5050
- POSTGRES_PASSWORD=
51+
- POSTGRES_DB=db
5152

5253
### Non-standard database names
5354

5455
If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`:
5556

5657
environment:
57-
- POSTGRES_MULTIPLE_DATABASES="test-db-1","test-db-2"
58+
- POSTGRES_MULTIPLE_DATABASES="test-db-1:user:pwd","test-db-2:user:pwd"

create-multiple-postgresql-databases.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ set -e
44
set -u
55

66
function create_user_and_database() {
7-
local database=$1
7+
local dbinfo=$1
8+
IFS=":" read -r database user password <<< "$dbinfo"
89
echo " Creating user and database '$database'"
10+
echo "Creating database '$database' with user '$user' and password '$password'"
911
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "postgres" <<-EOSQL
10-
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
12+
SELECT 'CREATE USER ' || LOWER(TRIM('$user')) AS create_user_query WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = LOWER(TRIM('$user')));\gexec
13+
ALTER USER $user WITH ENCRYPTED PASSWORD '$password';
1114
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
12-
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
15+
GRANT ALL PRIVILEGES ON DATABASE $database TO $user;
1316
EOSQL
1417
}
1518

0 commit comments

Comments
 (0)