#!/bin/bash if [ "$(id -u)" != "0" ]; then echo -e "\033[31mThis script requires superuser rights\033[0m" exit 0 fi if [ -z "$1" ] || [ -z "$2" ]; then echo "Please provide all arguments: CREATE_DB_NAME, CREATE_DB,_PASSWORD" exit 1 fi trap 'echo -e "\033[31mAn error has occurred\033[0m"; exit 1' EXIT set -e export DEBIAN_FRONTEND=noninteractive CREATE_DB_NAME=$1 CREATE_DB_PASSWORD=$2 : "${POSTGRES_USER:?}" : "${POSTGRES_DB:?}" sudo docker exec -i postgres psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL CREATE USER "$CREATE_DB_NAME" WITH ENCRYPTED PASSWORD '$CREATE_DB_PASSWORD'; CREATE DATABASE "$CREATE_DB_NAME"; GRANT ALL PRIVILEGES ON DATABASE "$CREATE_DB_NAME" TO "$CREATE_DB_NAME"; ALTER DATABASE "$CREATE_DB_NAME" OWNER TO "$CREATE_DB_NAME"; EOSQL trap - EXIT echo -e "\033[32mDatabase $CREATE_DB_NAME created successfully\033[0m"