-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_postgres.sh
52 lines (42 loc) · 1.38 KB
/
start_postgres.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
#!/bin/bash
DB_NAME=${POSTGRES_DB:-}
DB_USER=${POSTGRES_USER:-}
DB_PASS=${POSTGRES_PASSWORD:-}
PG_CONFDIR="/var/lib/pgsql/data"
__create_user() {
#Grant rights
usermod -G wheel postgres
# Check to see if we have pre-defined credentials to use
if [ -n "${DB_USER}" ]; then
if [ -z "${DB_PASS}" ]; then
echo ""
echo "WARNING: "
echo "No password specified for \"${DB_USER}\". Generating one"
echo ""
DB_PASS=$(pwgen -c -n -1 12)
echo "Password for \"${DB_USER}\" created as: \"${DB_PASS}\""
fi
echo "Creating user \"${DB_USER}\"..."
echo "CREATE ROLE ${DB_USER} with CREATEROLE login superuser PASSWORD '${DB_PASS}';" |
sudo -u postgres -H postgres --single \
-c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}
fi
if [ -n "${DB_NAME}" ]; then
echo "Creating database \"${DB_NAME}\"..."
echo "CREATE DATABASE ${DB_NAME};" | \
sudo -u postgres -H postgres --single \
-c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}
if [ -n "${DB_USER}" ]; then
echo "Granting access to database \"${DB_NAME}\" for user \"${DB_USER}\"..."
echo "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} to ${DB_USER};" |
sudo -u postgres -H postgres --single \
-c config_file=${PG_CONFDIR}/postgresql.conf -D ${PG_CONFDIR}
fi
fi
}
__run_supervisor() {
supervisord -n
}
# Call all functions
__create_user
__run_supervisor