You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

120 lines
4.1 KiB

#!/bin/bash
echo "####################################################################"
echo "# docker_postgres_setup_script #"
echo "# Dennis Buchhorn - bucde@b-eit.de #"
echo "####################################################################"
echo "# WARNING: password is plain text input! #"
echo "# #"
echo "# Config will be saved in 'config' file. #"
echo "# (everything in plain text, even the password) #"
#echo "# #"
echo "####################################################################"
echo ""
DATE=$(date +'%y%m%d%H%M%S')
CONFIG_EXISTS="false"
USE_EXISTING_CONFIG=""
DELETE_CONFIG=""
POSTGRES_PROJECT_NAME="postgres_${DATE}"
POSTGRES_PROJECT_NAME_SUFFIX=""
POSTGRES_CONTAINER_NAME="${POSTGRES_PROJECT_NAME}_db"
POSTGRES_VERSION=""
POSTGRES_HOST_IP_ADDRESS=""
POSTGRES_HOST_PORT=""
POSTGRES_ADMIN_USER=""
POSTGRES_ADMIN_PASSWORD=""
POSTGRES_SHARED_HOSTING=""
read -p "Enter postgres project name suffix (leave empty for none): " POSTGRES_PROJECT_NAME_SUFFIX
if [ -n "$POSTGRES_PROJECT_NAME_SUFFIX" ]; then
POSTGRES_PROJECT_NAME="${POSTGRES_PROJECT_NAME}_${POSTGRES_PROJECT_NAME_SUFFIX}"
POSTGRES_CONTAINER_NAME="${POSTGRES_CONTAINER_NAME}_${POSTGRES_PROJECT_NAME_SUFFIX}"
fi
rm -f .env
touch .env
echo "COMPOSE_PROJECT_NAME="$POSTGRES_PROJECT_NAME >> .env
echo "POSTGRES_CONTAINER_NAME="$POSTGRES_CONTAINER_NAME >> .env
if [ -f "config" ]; then
CONFIG_EXISTS="true"
read -p "Config file 'config' already exists! Would you like to use this? (y/n)" USE_EXISTING_CONFIG
fi
if [ $CONFIG_EXISTS == "false" ] || [ $USE_EXISTING_CONFIG == "n" ]; then
read -p "Enter postgres version which should be used: " POSTGRES_VERSION
read -p "Enter host (vm) ip address on which postgres should be accessible: " POSTGRES_HOST_IP_ADDRESS
read -p "Enter host (vm) port on which postgres should be accessible: " POSTGRES_HOST_PORT
read -p "Enter admin user name for postgres: " POSTGRES_ADMIN_USER
read -p "Enter admin password for postgres: " POSTGRES_ADMIN_PASSWORD
rm -f config
touch config
echo "POSTGRES_VERSION="$POSTGRES_VERSION >> config
echo "POSTGRES_HOST_IP_ADDRESS="$POSTGRES_HOST_IP_ADDRESS >> config
echo "POSTGRES_HOST_PORT="$POSTGRES_HOST_PORT >> config
echo "POSTGRES_ADMIN_USER="$POSTGRES_ADMIN_USER >> config
echo "POSTGRES_ADMIN_PASSWORD="$POSTGRES_ADMIN_PASSWORD >> config
echo "Config file 'config' created!"
else
echo "Use existing config file 'config'!"
while read line; do
if [[ $line == *"POSTGRES_ADMIN_USER="* ]]; then
POSTGRES_ADMIN_USER=$(echo $line | cut -c 21-)
fi
done < "config"
fi
cat "config" >> ".env"
read -p "Should the config file 'config' be deleted after creating the container? (y/n)" DELETE_CONFIG
read -p "Would you like to modify postgres rights for shared hosting? (y/n)" POSTGRES_SHARED_HOSTING
docker-compose up -d
echo -n "Wait until postgres is ready ..."
while ! docker exec $POSTGRES_CONTAINER_NAME pg_isready; do
echo -n "."
sleep 1
done
echo -e "\nPostgres is ready!"
if [ $POSTGRES_SHARED_HOSTING == "y" ]; then
rm -f tmp
touch tmp
echo "REVOKE ALL ON DATABASE template1 FROM public;" >> tmp
echo "REVOKE ALL ON SCHEMA public FROM public;" >> tmp
echo "GRANT ALL ON SCHEMA public TO "$POSTGRES_ADMIN_USER";" >> tmp
echo "REVOKE ALL ON pg_user FROM public;" >> tmp
echo "REVOKE ALL ON pg_roles FROM public;" >> tmp
echo "REVOKE ALL ON pg_group FROM public;" >> tmp
echo "REVOKE ALL ON pg_authid FROM public;" >> tmp
echo "REVOKE ALL ON pg_auth_members FROM public;" >> tmp
echo "REVOKE ALL ON pg_database FROM public;" >> tmp
echo "REVOKE ALL ON pg_tablespace FROM public;" >> tmp
echo "REVOKE ALL ON pg_settings FROM public;" >> tmp
cat tmp | docker exec -i postgres psql -U $POSTGRES_ADMIN_USER -d template1
rm -f tmp
fi
if [ $DELETE_CONFIG == "y" ]; then
rm -f config
echo "Config file 'config' deleted!"
fi
rm -f .env
echo "Done!"