Skip to content

Instantly share code, notes, and snippets.

@beldpro-ci
Created April 29, 2017 14:56
Show Gist options
  • Save beldpro-ci/bc8d1a48f6a012a1b494460aac84796a to your computer and use it in GitHub Desktop.
Save beldpro-ci/bc8d1a48f6a012a1b494460aac84796a to your computer and use it in GitHub Desktop.
Initialization script for `postgre:alpine`
#!/bin/bash
# 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
# 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=(
"FILLA_DB_USER"
"FILLA_DB_PASSWORD"
"FILLA_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 $FILLA_DB_USER WITH PASSWORD '$FILLA_DB_PASSWORD';
CREATE DATABASE $FILLA_DB_DATABASE;
GRANT ALL PRIVILEGES ON DATABASE $FILLA_DB_DATABASE TO $FILLA_DB_USER;
EOSQL
}
# 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 "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment