36
|
1 #!/bin/bash
|
|
2
|
|
3 export PGUSER=postgres
|
|
4 TEST=`psql <<- EOSQL
|
|
5 SELECT 1 FROM pg_database WHERE datname='$DB_NAME';
|
|
6 EOSQL`
|
|
7
|
|
8 if [[ $TEST == "1" ]]; then
|
|
9 echo "$DB_NAME exists. continuing startup."
|
|
10 # database exists
|
|
11 # $? is 0
|
|
12 exit 0
|
|
13 else
|
|
14 echo "Creating $DB_NAME for $DB_USER"
|
|
15 psql <<- EOSQL
|
|
16 CREATE ROLE $DB_USER WITH LOGIN ENCRYPTED PASSWORD '${DB_PASS}' CREATEDB;
|
|
17 CREATE DATABASE $DB_NAME WITH OWNER $DB_USER TEMPLATE template0 ENCODING 'UTF8';
|
|
18 GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
|
|
19 EOSQL
|
|
20
|
|
21 echo "Restoring backup file"
|
|
22
|
|
23 gunzip -f $BACKUP_FILE -c | psql
|
|
24
|
|
25 fi
|
|
26
|
|
27 echo ""
|
|
28 echo "$DB_NAME database created"
|