docker-image-postgres/custom-entrypoint.sh
Mauro Torrez ad2d0cfcb0
All checks were successful
continuous-integration/drone/tag Build is passing
entrypoint - fix heredoc
2020-11-22 18:56:55 -03:00

114 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeo pipefail
# This script extends the original entrypoint with support for adding
# a non-superuser owner to the $POSTGRES_DB database
# source vendor entrypoint
. /usr/local/bin/docker-entrypoint.sh
# create initial database
# uses environment variables for input: POSTGRES_DB
docker_setup_db() {
if [ "$POSTGRES_DB_USER" != 'postgres' ]; then
if [ -n "$POSTGRES_DB_PASSWORD" ]; then
POSTGRES_DB= docker_process_sql --dbname postgres --set user="$POSTGRES_DB_USER" --set password="$POSTGRES_DB_PASSWORD" <<EOSQL
CREATE USER :"user" WITH PASSWORD :'password' ;
EOSQL
echo
else
POSTGRES_DB= docker_process_sql --dbname postgres --set user="$POSTGRES_DB_USER" <<EOSQL
CREATE USER :"user" ;
EOSQL
echo
fi
if [ "$POSTGRES_DB" != 'postgres' ]; then
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --set user="$POSTGRES_DB_USER" <<EOSQL
CREATE DATABASE :"db" WITH OWNER = :"user" ;
EOSQL
echo
fi
else
if [ "$POSTGRES_DB" != 'postgres' ]; then
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<EOSQL
CREATE DATABASE :"db" ;
EOSQL
echo
fi
fi
}
# Loads various settings that are used elsewhere in the script
# This should be called before any other functions
docker_setup_env() {
file_env 'POSTGRES_PASSWORD'
file_env 'POSTGRES_USER' 'postgres'
file_env 'POSTGRES_DB' "$POSTGRES_USER"
file_env 'POSTGRES_INITDB_ARGS'
# default authentication method is md5
: "${POSTGRES_HOST_AUTH_METHOD:=md5}"
file_env 'POSTGRES_DB_USER' "$POSTGRES_USER"
file_env 'POSTGRES_DB_PASSWORD'
declare -g DATABASE_ALREADY_EXISTS
# look specifically for PG_VERSION, as it is expected in the DB dir
if [ -s "$PGDATA/PG_VERSION" ]; then
DATABASE_ALREADY_EXISTS='true'
fi
}
_main() {
# if first arg looks like a flag, assume we want to run postgres server
if [ "${1:0:1}" = '-' ]; then
set -- postgres "$@"
fi
if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then
docker_setup_env
# setup data directories and permissions (when run as root)
docker_create_db_directories
if [ "$(id -u)" = '0' ]; then
# then restart script as postgres user
exec gosu postgres "$BASH_SOURCE" "$@"
fi
# only run initialization on an empty data directory
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
docker_verify_minimum_env
# check dir permissions to reduce likelihood of half-initialized database
ls /docker-entrypoint-initdb.d/ > /dev/null
docker_init_database_dir
pg_setup_hba_conf
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
docker_temp_server_start "$@"
docker_setup_db
docker_process_init_files /docker-entrypoint-initdb.d/*
docker_temp_server_stop
unset PGPASSWORD
echo
echo 'PostgreSQL init process complete; ready for start up.'
echo
else
echo
echo 'PostgreSQL Database directory appears to contain a database; Skipping initialization'
echo
fi
fi
exec "$@"
}
if ! _is_sourced; then
_main "$@"
fi