Skip to content

Commit

Permalink
Allow creating multiple databases
Browse files Browse the repository at this point in the history
  • Loading branch information
schnapster committed Oct 3, 2021
1 parent 69e3a32 commit 9989db2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions initdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ while ! pg_isready -U "$POSTGRES_USER"; do
sleep 1
done

# make sure the role exists
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -tAc "SELECT 1 FROM pg_roles WHERE rolname='$ROLE';" | grep -q 1 || createuser -U "$POSTGRES_USER" "$ROLE"
PSQL="psql -v ON_ERROR_STOP=1 -U $POSTGRES_USER"

# make sure the role exists
$PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname='$ROLE';" | grep -q 1 || createuser -U "$POSTGRES_USER" "$ROLE"
# make sure the role has a password
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -tAc "ALTER USER $ROLE WITH PASSWORD '$PASSWORD';"
$PSQL -tAc "ALTER USER $ROLE WITH PASSWORD '$PASSWORD';"

for database in $DB; do
# make sure the database exists
$PSQL -tc "SELECT 1 FROM pg_database WHERE datname = '$database';" | grep -q 1 || $PSQL -c "CREATE DATABASE $database WITH OWNER = $ROLE;"
# make sure the database is owned by the role
$PSQL -c "ALTER DATABASE $database OWNER TO $ROLE;"
$PSQL -c "GRANT ALL PRIVILEGES ON DATABASE $database TO $ROLE;"

# make sure the database exists
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -tc "SELECT 1 FROM pg_database WHERE datname = '$DB';" | grep -q 1 || psql -U "$POSTGRES_USER" -c "CREATE DATABASE $DB WITH OWNER = $ROLE;"
# make sure the database is owned by the role
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "ALTER DATABASE $DB OWNER TO $ROLE;"
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "GRANT ALL PRIVILEGES ON DATABASE $DB TO $ROLE;"
# make sure extensions are set up
for extension in $EXTENSIONS; do
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$DB" -c "CREATE EXTENSION IF NOT EXISTS $extension;"
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$DB" -c "ALTER EXTENSION $extension SET SCHEMA pg_catalog;"
# make sure extensions are set up
for extension in $EXTENSIONS; do
$PSQL -d "$database" -c "CREATE EXTENSION IF NOT EXISTS $extension WITH SCHEMA pg_catalog;"
$PSQL -d "$database" -c "ALTER EXTENSION $extension SET SCHEMA pg_catalog;"
done
done

# Credits: https://twitter.com/samokhvalov/status/732359133010092032
Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# We are using this script, because we need to reliably execute a script on each start of the db container,
# to migrate the database for containers with an external volume persisting the database data, where the scripts in
# /docker-entrypoint-initdb.d/ are getting skipped.
# /docker-entrypoint-initdb.d/ are getting skipped, or add databases & extensions.
# Relevant issue: https://github.com/docker-library/postgres/issues/191

set -e
Expand Down

0 comments on commit 9989db2

Please sign in to comment.