-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
postgreshelpers.sh
104 lines (84 loc) · 2.12 KB
/
postgreshelpers.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
clear;
BACKUP_OLD_DB=$4;
DB_NAME=$1;
DB_USER=$2;
PASSWORD=$3
DBS_PATH="$HOME/pg_databases";
NEW_DB="$DBS_PATH/$DB_NAME";
export LC_CTYPE=pt_BR.UTF-8;
start_db () {
cd $DBS_PATH;
pg_ctl -D "$DB_NAME" -l logfile start
cd -;
};
stop_db () {
cd $DBS_PATH;
pg_ctl -D "$DB_NAME" -l logfile stop
cd -;
sleep 3;
killall -9 postgres;
};
if [ $2 == "start" ]; then
start_db
exit;
fi;
if [ $2 == "stop" ]; then
stop_db
exit;
fi;
echo DB Name: ${DB_NAME:?You need to provide a name for the database as the first param}
echo DB_USER: ${DB_USER:?You need to provide a name for the db user as the second param}
echo DB PASSWORD: ${PASSWORD?You need to provide a password for db as the third param}
init_folder() {
if [ -d $NEW_DB ]; then
echo "DBs folder: $DBS_PATH";
else
mkdir -p $DBS_PATH;
echo "Created DBs folder in $DBS_PATH"
fi;
}
init_db () {
cd $DBS_PATH;
if [ -d $NEW_DB ]; then
if [ $BACKUP_OLD_DB == 1 ]; then
mv $NEW_DB "$NEW_DB"".bkp.$(date +"%s")";
else
rm -rf $NEW_DB;
fi
initdb $DB_NAME;
else
initdb $DB_NAME;
fi
pg_ctl -D "$DB_NAME" -l logfile start;
cd -;
};
recreate_db() {
# stop db process
psql -h localhost postgres ${USER} <<- _EOF_
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '${DB_NAME}'
AND pid <> pg_backend_pid();
\q
_EOF_
psql -h localhost postgres ${USER} <<- _EOF_
DROP DATABASE ${DB_NAME};
CREATE DATABASE ${DB_NAME};
DROP ROLE ${DB_USER};
CREATE USER ${DB_USER} WITH PASSWORD '${PASSWORD}';
GRANT ALL PRIVILEGES ON DATABASE "${DB_NAME}" to ${DB_USER};
ALTER USER ${DB_USER} WITH SUPERUSER;
DROP DATABASE ${DB_NAME}_test;
CREATE DATABASE ${DB_NAME}_test;
CREATE USER ${DB_USER} WITH PASSWORD '${PASSWORD}';
GRANT ALL PRIVILEGES ON DATABASE "${DB_NAME}_test" to ${DB_USER};
ALTER USER ${DB_USER} WITH SUPERUSER;
_EOF_
}
stop_db;
init_folder;
init_db;
recreate_db;
stop_db;
start_db;