Repository navigation
Expand file tree
/
Copy pathstart_postgres.sh
More file actions
135 lines (115 loc) · 3.55 KB
/
Copy pathstart_postgres.sh
File metadata and controls
135 lines (115 loc) · 3.55 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
set -e
# Default parameters
#
DATADIR=${PG_DATADIR:=/var/lib/postgresql/%%PG_VERSION%%/main}
BINDIR=${PG_BINDIR:=/usr/lib/postgresql/%%PG_VERSION%%/bin}
CONFIG_FILE=${PG_CONFIG_FILE:=/etc/postgresql/%%PG_VERSION%%/main/postgresql.conf}
HBA_FILE=${PG_HBA_FILE:=/etc/postgresql/%%PG_VERSION%%/main/pg_hba.conf}
IDENT_FILE=${PG_IDENT_FILE:=/etc/postgresql/%%PG_VERSION%%/main/pg_ident.conf}
OUT_LOG=/var/log/postgresql/out.log
# Custom die function.
#
die() { echo >&2 -e "\nRUN ERROR: $@\n"; exit 1; }
StartPGServer()
{
echo "=> Starting Postgres Server ..."
PGARGS="-c config_file=$CONFIG_FILE -c data_directory=$DATADIR -c hba_file=$HBA_FILE -c ident_file=$IDENT_FILE"
su postgres sh -c "$BINDIR/postgres ${PGARGS} ${EXTRA_OPTS} > $OUT_LOG 2>&1 &"
# Time out in 1 minute
LOOP_LIMIT=60
for (( i=0 ; ; i++ )); do
if [ ${i} -eq ${LOOP_LIMIT} ]; then
echo "Time out. PostgreSQL still has not started ..."
exit 1
fi
echo "=> Waiting for confirmation of PostgreSQL service startup, trying ${i}/${LOOP_LIMIT} ..."
sleep 1
pg_isready -q -U postgres
if [ $? == 0 ]; then
break
fi
done
}
InitDB() {
echo "=> Initializing DB ..."
# If DATADIR does not exist, create it
if [ ! -d $DATADIR ]; then
echo "Creating Postgres data at $DATADIR"
mkdir -p $DATADIR
fi
# If DATADIR has no content, initialize it
if [ ! "$(ls -A $DATADIR)" ]; then
echo "Initializing Postgres Database at $DATADIR"
chown -R postgres $DATADIR
su postgres sh -c "$BINDIR/initdb $DATADIR"
touch $DATADIR/.EMPTY_DB
fi
}
CreatePGUser()
{
SHOW_PWD=false
if [ "$PG_PASS" = "**Random**" ]; then
unset PG_PASS
SHOW_PWD=true
fi
PASS=${PG_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${PG_PASS} ] && echo "preset" || echo "random" )
echo "=> Creating PostgreSQL user ${PG_USER} ..."
psql -U postgres -c "CREATE USER ${PG_USER} WITH SUPERUSER ENCRYPTED PASSWORD '${PASS}';"
echo "=> Done!"
if [ "$SHOW_PWD" = true ]; then
echo "========================================================================"
echo "You can now connect to this PostgreSQL Server using:"
echo ""
echo " psql -U $PG_USER -W -h <host> -p <port> -d <database>"
echo " Randomly assigned password: $PASS"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "User 'postgres' has no password but only allows local connections"
echo "========================================================================"
fi
}
OnCreateDB()
{
if [ "${ON_CREATE_DB}" = "**False**" ]; then
unset ON_CREATE_DB
else
echo "Creating PostgreSQL database ${ON_CREATE_DB}"
psql -U postgres -c "CREATE DATABASE ${ON_CREATE_DB};"
echo "Database created!"
fi
}
ImportSql()
{
if [ -z "${ON_CREATE_DB}" ]; then
echo "=> Cannot import SQL files. ON_CREATE_DB cannot be empty"
else
for FILE in ${STARTUP_SQL}; do
echo "=> Importing SQL file ${FILE}"
psql -U postgres $ON_CREATE_DB < ${FILE}
done
fi
}
echo "=> Starting PostgreSQL ..."
InitDB
StartPGServer
# Create admin user and pre create database
if [ -f $DATADIR/.EMPTY_DB ]; then
CreatePGUser
OnCreateDB
rm $DATADIR/.EMPTY_DB
fi
# Import Startup SQL
if [ -n "${STARTUP_SQL}" ]; then
if [ ! -f /sql_imported ]; then
echo "=> Initializing DB with ${STARTUP_SQL}"
ImportSql
touch /sql_imported
fi
fi
# Set backup schedule
if [ -n "${CRON_TIME}" ]; then
exec /enable_backups.sh
fi
tail -f $OUT_LOG