@@ -247,9 +247,6 @@ initialize_database() {
247247 set_postgresql_param " log_directory" " ${PG_LOGDIR} "
248248 set_postgresql_param " log_filename" " postgresql-${PG_VERSION} -main.log"
249249
250- # listen on all interfaces
251- set_postgresql_param " listen_addresses" " *"
252-
253250 # allow remote connections to postgresql database
254251 set_hba_param " host all all 0.0.0.0/0 md5"
255252}
@@ -261,6 +258,11 @@ trust_localnet() {
261258 fi
262259}
263260
261+ set_resolvconf_perms () {
262+ echo " Setting resolv ACLs..."
263+ setfacl -m user:${PG_USER} :r /etc/resolv.conf
264+ }
265+
264266configure_recovery () {
265267 if [[ ${REPLICATION_MODE} == slave ]]; then
266268 echo " Configuring recovery..."
@@ -295,8 +297,9 @@ create_user() {
295297 exit 1
296298 fi
297299 echo " Creating database user: ${DB_USER} "
298- echo " CREATE ROLE \" ${DB_USER} \" with LOGIN CREATEDB PASSWORD '${DB_PASS} ';" | \
299- exec_as_postgres ${PG_BINDIR} /postgres --single -D ${PG_DATADIR} > /dev/null 2>&1
300+ if [[ -z $( psql -U ${PG_USER} -Atc " SELECT 1 FROM pg_catalog.pg_user WHERE usename = '${DB_USER} '" ; ) ]]; then
301+ psql -U ${PG_USER} -c " CREATE ROLE \" ${DB_USER} \" with LOGIN CREATEDB PASSWORD '${DB_PASS} ';" > /dev/null
302+ fi
300303 ;;
301304 esac
302305 fi
@@ -312,17 +315,16 @@ create_database() {
312315 echo -n " Creating database(s): "
313316 for database in $( awk -F' ,' ' {for (i = 1 ; i <= NF ; i++) print $i}' <<< " ${DB_NAME}" ) ; do
314317 echo -n " ${database} "
315- echo " CREATE DATABASE \" ${database} \" ;" | \
316- exec_as_postgres ${PG_BINDIR} /postgres --single -D ${PG_DATADIR} > /dev/null 2>&1
318+ if [[ -z $( psql -U ${PG_USER} -Atc " SELECT 1 FROM pg_catalog.pg_database WHERE datname = '${DB_NAME} '" ; ) ]]; then
319+ psql -U ${PG_USER} -c " CREATE DATABASE \" ${database} \" ;" > /dev/null
320+ fi
317321
318322 if [[ ${DB_UNACCENT} == true ]]; then
319- echo " CREATE EXTENSION IF NOT EXISTS unaccent;" | \
320- exec_as_postgres ${PG_BINDIR} /postgres --single ${database} -D ${PG_DATADIR} > /dev/null 2>&1
323+ psql -U ${PG_USER} -d ${database} -c " CREATE EXTENSION IF NOT EXISTS unaccent;" > /dev/null 2>&1
321324 fi
322325
323326 if [[ -n ${DB_USER} ]]; then
324- echo " GRANT ALL PRIVILEGES ON DATABASE \" ${database} \" to \" ${DB_USER} \" ;" | \
325- exec_as_postgres ${PG_BINDIR} /postgres --single -D ${PG_DATADIR} > /dev/null 2>&1
327+ psql -U ${PG_USER} -c " GRANT ALL PRIVILEGES ON DATABASE \" ${database} \" to \" ${DB_USER} \" ;" > /dev/null
326328 fi
327329 done
328330 echo
@@ -342,16 +344,31 @@ create_replication_user() {
342344 fi
343345
344346 echo " Creating replication user: ${REPLICATION_USER} "
345- echo " CREATE ROLE \" ${REPLICATION_USER} \" WITH REPLICATION LOGIN ENCRYPTED PASSWORD '${REPLICATION_PASS} ';" | \
346- exec_as_postgres ${PG_BINDIR} /postgres --single -D ${PG_DATADIR} > /dev/null 2>&1
347+ psql -U ${PG_USER} -c " CREATE ROLE \" ${REPLICATION_USER} \" WITH REPLICATION LOGIN ENCRYPTED PASSWORD '${REPLICATION_PASS} ';" > /dev/null
347348
348349 set_hba_param " host replication ${REPLICATION_USER} 0.0.0.0/0 md5"
349350 ;;
350351 esac
351352 fi
352353}
353354
354- set_resolvconf_perms () {
355- echo " Setting resolv ACLs..."
356- setfacl -m user:${PG_USER} :r /etc/resolv.conf
355+ configure_postgresql () {
356+ initialize_database
357+ configure_recovery
358+ configure_ssl
359+ trust_localnet
360+
361+ # start postgres server internally for the creation of users and databases
362+ set_postgresql_param " listen_addresses" " 127.0.0.1" quiet
363+ exec_as_postgres ${PG_BINDIR} /pg_ctl -D ${PG_DATADIR} -w start > /dev/null
364+
365+ create_user
366+ create_database
367+ create_replication_user
368+
369+ # stop the postgres server
370+ exec_as_postgres ${PG_BINDIR} /pg_ctl -D ${PG_DATADIR} -w stop > /dev/null
371+
372+ # listen on all interfaces
373+ set_postgresql_param " listen_addresses" " *" quiet
357374}
0 commit comments