Postgresql user and database creation script.

master
Philippe PITTOLI 2019-10-24 12:03:40 +02:00
parent 7f9bd275b1
commit b4b5754e7c
1 changed files with 117 additions and 0 deletions

117
utils/pg_create_user.sh Executable file
View File

@ -0,0 +1,117 @@
#!/usr/bin/env zsh
: ${PGHOST:=localhost}
: ${PGUSER:=postgres}
# TODO: add default postgresql port
: ${pgport}
: ${dbname}
: ${command}
: ${cmdparameters}
: ${simulation}
function usage() {
cat <<END
usage: $0 [OPTIONS] command [parameters]
OPTIONS:
--pghost=<...> postgresql hostname
--pgport=<...> postgresql port
--pgdatadir=<...> postgresql data dir
--pguser=<...> postgresql username
--dbuser=<...> database username
--dbpassfile=<...> database password file
--dbname=<...> database name
--simulation print commands
command:
create_user
create_db
create_user_and_db
END
}
function print_create_user() {
cat <<END
# user creation
psql -p ${pgport} -c "CREATE USER ${dbuser};"
psql -p ${pgport} -c "ALTER USER ${dbuser} WITH ENCRYPTED PASSWORD '${dbpass}';"
END
}
function create_user() {
su postgres -c "
psql -p ${pgport} -c \"CREATE USER ${dbuser};\"
psql -p ${pgport} -c \"ALTER USER ${dbuser} WITH ENCRYPTED PASSWORD '${dbpass}';\"
"
}
function print_create_db() {
cat <<END
# database creation
psql -p ${pgport} -c "CREATE DATABASE ${dbname} OWNER ${dbuser};"
END
}
function create_db() {
su postgres -c "psql -p ${pgport} -c \"CREATE DATABASE ${dbname} OWNER ${dbuser};\""
}
for i
do
case $i in
(--pghost*) export PGHOST=$(echo ${i} | sed "s/--pghost=//") ;;
(--pgport*) export pgport=$(echo ${i} | sed "s/--pgport=//") ;;
(--pgdatadir*) export PGDATA=$(echo ${i} | sed "s/--pgdatadir=//") ;;
(--pguser*) export PGUSER=$(echo ${i} | sed "s/--pguser=//") ;;
(--dbuser*) export dbuser=$(echo ${i} | sed "s/--dbuser=//") ;;
(--dbpassfile*) dbpassfile=$(echo ${i} | sed "s/--dbpassfile=//") ;;
(--dbname*) export dbname=$(echo ${i} | sed "s/--dbname=//") ;;
(--simulation*) simulation="yes" ;;
(-*) echo "option ${i} not recognized" ; usage $0 ; exit 0 ;;
(*) [ -z $command ] && command=${i} || cmdparameters=(${cmdparameters} ${i})
esac
done
if [ -z ${dbpassfile} ]
then
echo "There is no password file for the database"
exit 1
fi
export dbpass="$(<$dbpassfile)"
lacking_information=
[ -z ${PGHOST} ] && lacking_information="yes" && echo "There is no pghost parameter"
[ -z ${pgport} ] && lacking_information="yes" && echo "There is no pgport parameter"
[ -z ${PGDATA} ] && lacking_information="yes" && echo "There is no datadir parameter"
[ -z ${PGUSER} ] && lacking_information="yes" && echo "There is no pguser parameter"
[ -z ${dbuser} ] && lacking_information="yes" && echo "There is no dbuser parameter"
[ -z ${dbpassfile} ] && lacking_information="yes" && echo "There is no passfile parameter"
[ -z ${dbname} ] && lacking_information="yes" && echo "There is no dbname parameter"
if [ ! -z ${lacking_information} ]
then
echo "Some information is missing"
exit 1
fi
if [ -z $simulation ]
then
case ${command} in
(create_db) create_db ;;
(create_user) create_user ;;
(create_user_and_db) create_user ; create_db ;;
(*) usage $0 ; exit 1 ;;
esac
else
case ${command} in
(create_db) print_create_db ;;
(create_user) print_create_user ;;
(create_user_and_db) print_create_user ; print_create_db ;;
(*) usage $0 ; exit 1 ;;
esac
fi