From a737b916d9dc1b4614b12ed317394e3208b3c788 Mon Sep 17 00:00:00 2001 From: Aaron Louie Date: Mon, 9 Mar 2020 14:57:15 -0400 Subject: [PATCH] Adds Dockerfile and initdb.sh --- Dockerfile | 7 +++++++ initdb.sh | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 Dockerfile create mode 100644 initdb.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fee1632 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM postgres +COPY ./initdb.sh:/docker-entrypoint-initdb.d/initdb.sh +EXPOSE 5432 +VOLUME ["/var/lib/postgresql/data"] +ENV POSTGRES_USER=$POSTGRES_USER +ENV POSTGRES_PASSWORD=$POSTGRES_PASSWORD +ENV POSTGRES_MULTIPLE_DATABASES=$POSTGRES_MULTIPLE_DATABASES diff --git a/initdb.sh b/initdb.sh new file mode 100644 index 0000000..aa665fa --- /dev/null +++ b/initdb.sh @@ -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