forked from PnX-SI/UsersHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_db.sh
executable file
·46 lines (41 loc) · 1.39 KB
/
install_db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
. config/settings.ini
function database_exists () {
# /!\ Will return false if psql can't list database. Edit your pg_hba.conf
# as appropriate.
if [ -z $1 ]
then
# Argument is null
return 0
else
# Grep db name in the list of database
sudo -n -u postgres -s -- psql -tAl | grep -q "^$1|"
return $?
fi
}
if database_exists $db_name
then
if $drop_apps_db
then
echo "Suppression de la base..."
sudo -n -u postgres -s dropdb $db_name
else
echo "La base de données existe et le fichier de settings indique de ne pas la supprimer."
fi
fi
if ! database_exists $db_name
then
mkdir -p log
echo "Création de la base..."
sudo -n -u postgres -s createdb -O $user_pg $db_name
echo "Ajout du language plpgsql..."
sudo -n -u postgres -s psql -d $db_name -c "CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';"
# Mise en place de la structure de la base et des données permettant son fonctionnement avec l'application
echo "Création de la base..."
export PGPASSWORD=$user_pg_pass;psql -h databases -U $user_pg -d $db_name -f data/usershub.sql &>> log/install_db.log
fi