Adds Dockerfile and initdb.sh

This commit is contained in:
Aaron Louie 2020-03-09 14:57:15 -04:00
parent 4547c66ccb
commit a737b916d9
2 changed files with 29 additions and 0 deletions

7
Dockerfile Normal file
View File

@ -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

22
initdb.sh Normal file
View File

@ -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