Skip to content

Commit

Permalink
Add Dockerfile and docker-compose to allow for multiple databases on …
Browse files Browse the repository at this point in the history
…one postgres container
  • Loading branch information
maxkadel committed Jun 2, 2020
1 parent 57ec6fa commit 3983487
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM postgres:12.3

COPY docker-entrypoint-initdb.d /docker-entrypoint-initdb.d
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# yul-dc-postgres
postgres image for yul-dc project
Postgres image for yul-dc project

Creates databases for Blacklight and Management projects in a single running container.
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3'

services:
db:
build: .
image: 'curationexperts/dc-postgres:master'
ports:
- "5432:5432"
volumes:
- "db:/var/lib/postgresql/data"
environment:
POSTGRES_MULTIPLE_DATABASES: blacklight_yul_development,yul_dc_management_development
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password

volumes:
db:
22 changes: 22 additions & 0 deletions docker-entrypoint-initdb.d/multiple-pg-databases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e
set -u

function create_user_and_database() {
local database=$1
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $database;
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
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
done
echo "Multiple databases created"
fi

0 comments on commit 3983487

Please sign in to comment.