Skip to content

Add shell script and guide #28

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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:pwdb1,db2:pwddb2
- POSTGRES_USER: myapp
- POSTGRES_PASSWORD:

Expand All @@ -45,7 +45,7 @@ to the container:
myapp-postgresql:
image: eu.gcr.io/your-project/postgres-multi-db
environment:
- POSTGRES_MULTIPLE_DATABASES: db1,db2
- POSTGRES_MULTIPLE_DATABASES: db1:pwdb1,db2:pwddb2
- POSTGRES_USER: myapp
- POSTGRES_PASSWORD:

Expand All @@ -54,4 +54,28 @@ to the container:
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":"test-db-pw-1","test-db-2":"test-db-pw-2"

## Add to docker composer as a service

You can pass the `POSTGRES_MULTIPLE_DATABASES` environment variable
to the docker composer like this:

```yaml
postgres:
image: postgres
container_name: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: your!!!Password
POSTGRES_MULTIPLE_DATABASES: db1:db1@Password, db2:db2@Password, db3:db3@Password
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
volumes:
- ./<path-of-the-entrypoint>/multiple-databases.sh:/docker-entrypoint-initdb.d/multiple-databases.sh
```
10 changes: 7 additions & 3 deletions create-multiple-postgresql-databases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ set -u

function create_user_and_database() {
local database=$1
local password=$2
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $database;
CREATE USER $database WITH PASSWORD '$password';
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL
Expand All @@ -16,7 +17,10 @@ EOSQL
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_user_and_database $db
IFS=':' read -ra PART <<< "$db"
db_name="${PART[0]}"
db_pass="${PART[1]}"
create_user_and_database $db_name $db_pass
done
echo "Multiple databases created"
fi
fi