Skip to content
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

Creation multiple users and databases #240

Closed
wants to merge 7 commits into from
Closed
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
77 changes: 73 additions & 4 deletions 9.2/alpine/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,29 @@ if [ "$1" = 'postgres' ]; then
file_env 'POSTGRES_INITDB_ARGS'
eval "su-exec postgres initdb $POSTGRES_INITDB_ARGS"

authMethod=trust
if [ "$POSTGRES_USERS" ]; then
USERS_ARR=$(echo $POSTGRES_USERS | tr "|" "\n")
for USER in $USERS_ARR
do
USER_PASSWORD=`echo $USER | cut -d: -f2`
if [ "$USER_PASSWORD" ]; then
authMethod=md5
fi
done
fi

# check password first so we can output the warning before postgres
# messes it up
file_env 'POSTGRES_PASSWORD'
if [ "$POSTGRES_PASSWORD" ]; then
pass="PASSWORD '$POSTGRES_PASSWORD'"
authMethod=md5
else
# The - option suppresses leading tabs but *not* spaces. :)
pass=
fi

if [ "$authMethod" == "trust" ]; then
cat >&2 <<-'EOWARN'
****************************************************
WARNING: No password has been set for the database.
Expand All @@ -62,9 +77,6 @@ if [ "$1" = 'postgres' ]; then
it in "docker run".
****************************************************
EOWARN

pass=
authMethod=trust
fi

{ echo; echo "host all all all $authMethod"; } | su-exec postgres tee -a "$PGDATA/pg_hba.conf" > /dev/null
Expand Down Expand Up @@ -99,6 +111,63 @@ if [ "$1" = 'postgres' ]; then

psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )

# If you want to create more than one user, please use that variable
# Variable example: POSTGRES_USERS="user1:user1pass|user2:user2pass|user3:user3password"
if [ "$POSTGRES_USERS" ]; then
USERS_ARR=$(echo $POSTGRES_USERS | tr "|" "\n")
for USER in $USERS_ARR
do
USER_NAME=`echo $USER | cut -d: -f1`
USER_PASSWORD=`echo $USER | cut -d: -f2`
if [ "$USER_NAME" = 'postgres' ]; then
op='ALTER'
else
op='CREATE'
fi
"${psql[@]}" --username postgres <<-EOSQL
$op USER "$USER_NAME" WITH SUPERUSER PASSWORD '$USER_PASSWORD' ;
EOSQL
done
fi

# If you want to create more than one database, please use that variable
# Variable example: POSTGRES_DATABASES="database1:user1|database2:user2|database3:user3"
if [ "$POSTGRES_DATABASES" ]; then
DATABASES_ARR=$(echo $POSTGRES_DATABASES | tr "|" "\n")
for DATABASE in $DATABASES_ARR
do
DATABASE_NAME=`echo $DATABASE | cut -d: -f1`
DATABASE_OWNER=`echo $DATABASE | cut -d: -f2`
if [ "$DATABASE_NAME" != 'postgres' ]; then
if [ "$DATABASE_OWNER" ]; then
"${psql[@]}" --username postgres <<-EOSQL
CREATE DATABASE "$DATABASE_NAME" owner "$DATABASE_OWNER" ;
EOSQL
echo
else
"${psql[@]}" --username postgres <<-EOSQL
CREATE DATABASE "$DATABASE_NAME" ;
EOSQL
echo
fi
fi
done
fi

# If you want to set up initial postgresql.conf parameters, please use that variable
# Variable example: POSTGRES_CONFIGS="work_mem:15MB|fsync:off|full_page_writes:off"
if [ "$POSTGRES_CONFIGS" ]; then
CONFIGS_ARR=$(echo $POSTGRES_CONFIGS | tr "|" "\n")
for CONFIG in $CONFIGS_ARR
do
CONFIG_NAME=`echo $CONFIG | cut -d: -f1`
CONFIG_VALUE=`echo $CONFIG | cut -d: -f2`
"${psql[@]}" --username postgres <<-EOSQL
ALTER SYSTEM SET $CONFIG_NAME = "$CONFIG_VALUE" ;
EOSQL
done
fi

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
Expand Down
77 changes: 73 additions & 4 deletions 9.2/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,29 @@ if [ "$1" = 'postgres' ]; then
file_env 'POSTGRES_INITDB_ARGS'
eval "gosu postgres initdb $POSTGRES_INITDB_ARGS"

authMethod=trust
if [ "$POSTGRES_USERS" ]; then
USERS_ARR=$(echo $POSTGRES_USERS | tr "|" "\n")
for USER in $USERS_ARR
do
USER_PASSWORD=`echo $USER | cut -d: -f2`
if [ "$USER_PASSWORD" ]; then
authMethod=md5
fi
done
fi

# check password first so we can output the warning before postgres
# messes it up
file_env 'POSTGRES_PASSWORD'
if [ "$POSTGRES_PASSWORD" ]; then
pass="PASSWORD '$POSTGRES_PASSWORD'"
authMethod=md5
else
# The - option suppresses leading tabs but *not* spaces. :)
pass=
fi

if [ "$authMethod" == "trust" ]; then
cat >&2 <<-'EOWARN'
****************************************************
WARNING: No password has been set for the database.
Expand All @@ -62,9 +77,6 @@ if [ "$1" = 'postgres' ]; then
it in "docker run".
****************************************************
EOWARN

pass=
authMethod=trust
fi

{ echo; echo "host all all all $authMethod"; } | gosu postgres tee -a "$PGDATA/pg_hba.conf" > /dev/null
Expand Down Expand Up @@ -99,6 +111,63 @@ if [ "$1" = 'postgres' ]; then

psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )

# If you want to create more than one user, please use that variable
# Variable example: POSTGRES_USERS="user1:user1pass|user2:user2pass|user3:user3password"
if [ "$POSTGRES_USERS" ]; then
USERS_ARR=$(echo $POSTGRES_USERS | tr "|" "\n")
for USER in $USERS_ARR
do
USER_NAME=`echo $USER | cut -d: -f1`
USER_PASSWORD=`echo $USER | cut -d: -f2`
if [ "$USER_NAME" = 'postgres' ]; then
op='ALTER'
else
op='CREATE'
fi
"${psql[@]}" --username postgres <<-EOSQL
$op USER "$USER_NAME" WITH SUPERUSER PASSWORD '$USER_PASSWORD' ;
EOSQL
done
fi

# If you want to create more than one database, please use that variable
# Variable example: POSTGRES_DATABASES="database1:user1|database2:user2|database3:user3"
if [ "$POSTGRES_DATABASES" ]; then
DATABASES_ARR=$(echo $POSTGRES_DATABASES | tr "|" "\n")
for DATABASE in $DATABASES_ARR
do
DATABASE_NAME=`echo $DATABASE | cut -d: -f1`
DATABASE_OWNER=`echo $DATABASE | cut -d: -f2`
if [ "$DATABASE_NAME" != 'postgres' ]; then
if [ "$DATABASE_OWNER" ]; then
"${psql[@]}" --username postgres <<-EOSQL
CREATE DATABASE "$DATABASE_NAME" owner "$DATABASE_OWNER" ;
EOSQL
echo
else
"${psql[@]}" --username postgres <<-EOSQL
CREATE DATABASE "$DATABASE_NAME" ;
EOSQL
echo
fi
fi
done
fi

# If you want to set up initial postgresql.conf parameters, please use that variable
# Variable example: POSTGRES_CONFIGS="work_mem:15MB|fsync:off|full_page_writes:off"
if [ "$POSTGRES_CONFIGS" ]; then
CONFIGS_ARR=$(echo $POSTGRES_CONFIGS | tr "|" "\n")
for CONFIG in $CONFIGS_ARR
do
CONFIG_NAME=`echo $CONFIG | cut -d: -f1`
CONFIG_VALUE=`echo $CONFIG | cut -d: -f2`
"${psql[@]}" --username postgres <<-EOSQL
ALTER SYSTEM SET $CONFIG_NAME = "$CONFIG_VALUE" ;
EOSQL
done
fi

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
Expand Down
77 changes: 73 additions & 4 deletions 9.3/alpine/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,29 @@ if [ "$1" = 'postgres' ]; then
file_env 'POSTGRES_INITDB_ARGS'
eval "su-exec postgres initdb $POSTGRES_INITDB_ARGS"

authMethod=trust
if [ "$POSTGRES_USERS" ]; then
USERS_ARR=$(echo $POSTGRES_USERS | tr "|" "\n")
for USER in $USERS_ARR
do
USER_PASSWORD=`echo $USER | cut -d: -f2`
if [ "$USER_PASSWORD" ]; then
authMethod=md5
fi
done
fi

# check password first so we can output the warning before postgres
# messes it up
file_env 'POSTGRES_PASSWORD'
if [ "$POSTGRES_PASSWORD" ]; then
pass="PASSWORD '$POSTGRES_PASSWORD'"
authMethod=md5
else
# The - option suppresses leading tabs but *not* spaces. :)
pass=
fi

if [ "$authMethod" == "trust" ]; then
cat >&2 <<-'EOWARN'
****************************************************
WARNING: No password has been set for the database.
Expand All @@ -62,9 +77,6 @@ if [ "$1" = 'postgres' ]; then
it in "docker run".
****************************************************
EOWARN

pass=
authMethod=trust
fi

{ echo; echo "host all all all $authMethod"; } | su-exec postgres tee -a "$PGDATA/pg_hba.conf" > /dev/null
Expand Down Expand Up @@ -99,6 +111,63 @@ if [ "$1" = 'postgres' ]; then

psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )

# If you want to create more than one user, please use that variable
# Variable example: POSTGRES_USERS="user1:user1pass|user2:user2pass|user3:user3password"
if [ "$POSTGRES_USERS" ]; then
USERS_ARR=$(echo $POSTGRES_USERS | tr "|" "\n")
for USER in $USERS_ARR
do
USER_NAME=`echo $USER | cut -d: -f1`
USER_PASSWORD=`echo $USER | cut -d: -f2`
if [ "$USER_NAME" = 'postgres' ]; then
op='ALTER'
else
op='CREATE'
fi
"${psql[@]}" --username postgres <<-EOSQL
$op USER "$USER_NAME" WITH SUPERUSER PASSWORD '$USER_PASSWORD' ;
EOSQL
done
fi

# If you want to create more than one database, please use that variable
# Variable example: POSTGRES_DATABASES="database1:user1|database2:user2|database3:user3"
if [ "$POSTGRES_DATABASES" ]; then
DATABASES_ARR=$(echo $POSTGRES_DATABASES | tr "|" "\n")
for DATABASE in $DATABASES_ARR
do
DATABASE_NAME=`echo $DATABASE | cut -d: -f1`
DATABASE_OWNER=`echo $DATABASE | cut -d: -f2`
if [ "$DATABASE_NAME" != 'postgres' ]; then
if [ "$DATABASE_OWNER" ]; then
"${psql[@]}" --username postgres <<-EOSQL
CREATE DATABASE "$DATABASE_NAME" owner "$DATABASE_OWNER" ;
EOSQL
echo
else
"${psql[@]}" --username postgres <<-EOSQL
CREATE DATABASE "$DATABASE_NAME" ;
EOSQL
echo
fi
fi
done
fi

# If you want to set up initial postgresql.conf parameters, please use that variable
# Variable example: POSTGRES_CONFIGS="work_mem:15MB|fsync:off|full_page_writes:off"
if [ "$POSTGRES_CONFIGS" ]; then
CONFIGS_ARR=$(echo $POSTGRES_CONFIGS | tr "|" "\n")
for CONFIG in $CONFIGS_ARR
do
CONFIG_NAME=`echo $CONFIG | cut -d: -f1`
CONFIG_VALUE=`echo $CONFIG | cut -d: -f2`
"${psql[@]}" --username postgres <<-EOSQL
ALTER SYSTEM SET $CONFIG_NAME = "$CONFIG_VALUE" ;
EOSQL
done
fi

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
Expand Down
Loading