forked from sameersbn/docker-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart
executable file
·87 lines (74 loc) · 3.06 KB
/
start
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
#!/bin/bash
set -e
PG_VERSION="9.1"
PG_CONFDIR="/etc/postgresql/${PG_VERSION}/main"
PG_BINDIR="/usr/lib/postgresql/${PG_VERSION}/bin"
PG_DATADIR="/var/lib/postgresql/${PG_VERSION}/main"
DB_NAME=${DB_NAME:-}
DB_USER=${DB_USER:-}
DB_PASS=${DB_PASS:-}
# fix permissions and ownership of /var/lib/postgresql
mkdir -p -m 0700 /var/lib/postgresql
chown -R postgres:postgres /var/lib/postgresql
# fix permissions and ownership of /run/postgresql
mkdir -p -m 0755 /run/postgresql
chown -R postgres:postgres /run/postgresql
chmod g+s /run/postgresql
# disable ssl
sed 's/ssl = true/#ssl = true/' -i ${PG_CONFDIR}/postgresql.conf
# listen on all interfaces
cat >> ${PG_CONFDIR}/postgresql.conf <<EOF
listen_addresses = '*'
EOF
# allow remote connections to postgresql database
cat >> ${PG_CONFDIR}/pg_hba.conf <<EOF
host all all 0.0.0.0/0 md5
EOF
# initialize PostgreSQL data directory
if [ ! -d ${PG_DATADIR} ]; then
echo "Initializing database..."
PG_PASSWORD=$(pwgen -c -n -1 14)
echo "${PG_PASSWORD}" > /var/lib/postgresql/pwfile
sudo -u postgres -H "${PG_BINDIR}/initdb" \
--pgdata="${PG_DATADIR}" --pwfile=/var/lib/postgresql/pwfile \
--username=postgres --encoding=unicode --auth=trust >/dev/null
fi
if [ -f /var/lib/postgresql/pwfile ]; then
PG_PASSWORD=$(cat /var/lib/postgresql/pwfile)
echo "|------------------------------------------------------------------|"
echo "| PostgreSQL User: postgres, Password: ${PG_PASSWORD} |"
echo "| |"
echo "| To remove the PostgreSQL login credentials from the logs, please |"
echo "| make a note of password and then delete the file pwfile |"
echo "| from the data store. |"
echo "|------------------------------------------------------------------|"
fi
if [ -n "${DB_USER}" ]; then
if [ -z "${DB_PASS}" ]; then
echo ""
echo "WARNING: "
echo " Please specify a password for \"${DB_USER}\". Skipping user creation..."
echo ""
DB_USER=
else
echo "Creating user \"${DB_USER}\"..."
echo "CREATE ROLE ${DB_USER} with LOGIN CREATEDB PASSWORD '${DB_PASS}';" |
sudo -u postgres -H ${PG_BINDIR}/postgres --single \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null 2>&1
fi
fi
if [ -n "${DB_NAME}" ]; then
echo "Creating database \"${DB_NAME}\"..."
echo "CREATE DATABASE ${DB_NAME};" | \
sudo -u postgres -H ${PG_BINDIR}/postgres --single \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null 2>&1
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 ${PG_BINDIR}/postgres --single \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null 2>&1
fi
fi
echo "Starting PostgreSQL server..."
exec sudo -u postgres -H ${PG_BINDIR}/postgres \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf