Skip to content

Instantly share code, notes, and snippets.

@akojimsg
Forked from ThinhPhan/Dockerfile
Created March 15, 2024 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akojimsg/226eb741a628d9168186818b103322ca to your computer and use it in GitHub Desktop.
Save akojimsg/226eb741a628d9168186818b103322ca to your computer and use it in GitHub Desktop.
Docker PostgreSQL - initial scripts - create user, create multiple databases.
#!/bin/bash
# Usage: Initial script put in `docker-entrypoint-initdb.d` directory
# Use bash for least error prones caused by shell syntax.
# Immediately exits if any error occurs during the script
# execution. If not set, an error could occur and the
# script would continue its execution.
set -o errexit
set -ex
# Creating an array that defines the environment variables
# that must be set. This can be consumed later via arrray
# variable expansion ${REQUIRED_ENV_VARS[@]}.
readonly REQUIRED_ENV_VARS=(
"DB_USER"
"DB_PASSWORD"
"DB_DATABASE"
"POSTGRES_USER")
# Main execution:
# - verifies if all environment variables are set
# - runs the SQL code to create user and database
main() {
check_env_vars_set
# init_user_and_db
}
# Checks if all of the required environment variables are set.
# If one of them isn't, echoes a text explaining which one isn't
# and the name of the ones that need to be.
check_env_vars_set() {
for required_env_var in "${REQUIRED_ENV_VARS[@]}"; do
if [[ -z "${!required_env_var}" ]]; then
echo "Error:
Environment variable '$required_env_var' not set.
Make sure you have the following environment variables set:"
"${REQUIRED_ENV_VARS[@]}"
"Aborting."
exit 1
fi
done
}
# Performs the initialization in the already-started PostgreSQL
# using the preconfigured POSTGRE_USER user.
# init_user_and_db() {
# psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
# CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
# CREATE DATABASE $DB_DATABASE;
# GRANT ALL PRIVILEGES ON DATABASE $DB_DATABASE TO $DB_USER;
# EOSQL
# }
function create_user_and_database() {
local DB_DATABASE=$1
echo " Creating user and database '$DB_DATABASE'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $DB_DATABASE WITH PASSWORD '${POSTGRES_PASSWORD}';
CREATE DATABASE $DB_DATABASE;
GRANT ALL PRIVILEGES ON DATABASE $DB_DATABASE TO $DB_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
# Executes the main routine with environment variables
# passed through the command line. We don't use them in
# this script but now you know
main "$@"
version: "3"
services:
db:
image: postgres:9.6.15-alpine
restart: always
environment:
- POSTGRES_MULTIPLE_DATABASES=mydb1,mydb2
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- ./db_data:/var/lib/postgresql/data
- ./postgres-initdb.sh:/docker-entrypoint-initdb.d/initdb.sh
- ./create-multiple-postgresql-databases.sh:/docker-entrypoint-initdb.d/01-create-db.sh
ports:
- "5432:5432"
networks:
- app-net
FROM postgres:9.15-alpine
COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment