Skip to content

Commit 4e936a9

Browse files
author
Sameer Naik
committed
removed use of single-user mode for the creation of users and databases
Single-user mode is meant for disaster recovery purposes and disables a lot of functionality. In this commit we removed the use of the single-user mode and instead start the postgres server internally to create users and databases.
1 parent e5adf68 commit 4e936a9

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
**latest**
4+
- removed use of single-user mode
5+
36
**9.4-11**
47
- added `PG_PASSWORD` variable to specify password for `postgres` user
58

entrypoint.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,7 @@ if [[ -z ${1} ]]; then
2424

2525
set_resolvconf_perms
2626

27-
initialize_database
28-
configure_recovery
29-
configure_ssl
30-
trust_localnet
31-
32-
create_user
33-
create_database
34-
create_replication_user
27+
configure_postgresql
3528

3629
echo "Starting PostgreSQL ${PG_VERSION}..."
3730
exec start-stop-daemon --start --chuid ${PG_USER}:${PG_USER} \

runtime/functions

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
264266
configure_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

Comments
 (0)