aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'postgresql.init')
-rw-r--r--postgresql.init165
1 files changed, 50 insertions, 115 deletions
diff --git a/postgresql.init b/postgresql.init
index fa0f90c..a00c9c9 100644
--- a/postgresql.init
+++ b/postgresql.init
@@ -3,19 +3,33 @@
# Distributed under the terms of the GNU General Public License v2
# $Header: $
-opts="${opts} reload"
+extra_started_command="reload"
+
+get_config() {
+ [ -f ${PGDATA}/postgresql.conf ] || return 1
+
+ eval echo $(sed -e 's:#.*::' ${PGDATA}/postgresql.conf | awk '$1 == "'$1'" { print $2 == "=" ? $3 : $2 }')
+}
depend() {
- use net
- provide postgresql
- provide postgresql-@SLOT@
+ use net
+ provide postgresql
+
+ if [ "$(get_config log_destination)" = "syslog" ]; then
+ use logger
+ fi
}
+configured_port=$(get_config port)
+: ${configured_port:=${PGPORT}}
+socket_path=$(get_config unix_socket_path)
+: ${socket_path:=@RUNDIR@/run/postgresql}
+
checkconfig() {
# Check that DATA_DIR has been set and exists
if [ -z ${DATA_DIR} ] ; then
eerror "DATA_DIR not set"
- eerror "HINT: Did you not update /etc/conf.d/postgresql-@SLOT@"
+ eerror "HINT: Perhaps you need to update /etc/conf.d/postgresql-@SLOT@"
fi
if [ ! -d ${DATA_DIR} ] ; then
eerror "Directory not found: ${DATA_DIR}"
@@ -32,10 +46,11 @@ checkconfig() {
[ ! -f ${PGDATA}/postgresql.conf ] && eerror " postgresql.conf"
[ ! -f ${PGDATA}/pg_hba.conf ] && eerror " pg_hba.conf"
[ ! -f ${PGDATA}/pg_ident.conf ] && eerror " pg_ident.conf"
- eerror "HINT: Try:"
- eerror "mv ${DATA_DIR}/*.conf ${PGDATA}"
+ eerror "HINT: Try:"
+ eerror " mv ${DATA_DIR}/*.conf ${PGDATA}"
return 1
fi
+
local file
local failed
for file in pg_hba pg_ident postgresql ; do
@@ -52,15 +67,11 @@ checkconfig() {
return 1
fi
- # Ensures @RUN@ exists for those who have it on tmpfs.
- if [ ! -d @RUN@ ] ; then
- mkdir -p @RUN@
- chown postgres:postgres @RUN@
- fi
- if [ -e @RUN@/.s.PGSQL.${PGPORT} ] ; then
- eerror "Socket conflict."
+ checkpath -d -m 0770 -o postgres:postgres ${socket_path}
+ if [ -e ${socket_path}/.s.PGSQL.${configured_port} ] ; then
+ eerror "Socket conflict."
eerror "A server is already listening on:"
- eerror " @RUN@/.s.PGSQL.${PGPORT}"
+ eerror " ${socket_path}/.s.PGSQL.${configured_port}"
eerror "HINT: Change PGPORT to listen on a different socket."
return 1
fi
@@ -71,17 +82,17 @@ start() {
ebegin "Starting PostgreSQL"
- if [ -f ${DATA_DIR}/postmaster.pid ] ; then
- rm -f ${DATA_DIR}/postmaster.pid
- fi
+ rm -f ${DATA_DIR}/postmaster.pid
local retval
- su -l postgres \
- -c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
- /usr/lib/postgresql-@SLOT@/bin/pg_ctl \
- start ${WAIT_FOR_START} -t ${START_TIMEOUT} -s -D ${DATA_DIR} \
- -o '-D ${PGDATA} --data-directory=${DATA_DIR} --silent-mode=true ${PGOPTS}'"
+ start-stop-daemon --start \
+ --user postgres \
+ --exec /usr/lib/postgresql-@SLOT@/bin/postgres \
+ --env "PGPORT=${configured_port} ${PG_EXTRA_ENV}" \
+ --wait $((${START_TIMEOUT}*1000)) \
+ --pidfile ${DATA_DIR}/postmaster.pid \
+ -- -D ${PGDATA} --data-directory=${DATA_DIR} --silent-mode=true ${PGOPTS}
retval=$?
if [ $retval -ne 0 ] ; then
@@ -90,112 +101,36 @@ start() {
return $retval
fi
- # The following is to catch the case of an already running server
- # in which pg_ctl doesn't know to which server it connected to and
- # falsely reports the server as 'up'
- if [ ! -f ${DATA_DIR}/postmaster.pid ] ; then
- eerror "The PID file doesn't exist but pg_ctl reported a running server."
- eerror "Please check whether there is another server running on the same port or read the log-file."
- eend 1
- return 1
- fi
-
eend $retval
}
stop() {
- ebegin "Stopping PostgreSQL (this can take up to $(( ${NICE_TIMEOUT} + ${RUDE_TIMEOUT} + ${FORCE_TIMEOUT} )) seconds)"
+ local seconds=$(( ${NICE_TIMEOUT} + ${RUDE_TIMEOUT} + ${FORCE_TIMEOUT} ))
+ ebegin "Stopping PostgreSQL (this can take up to ${seconds} seconds)"
local retval
-
- if [ "${NICE_QUIT}" != "NO" ] ; then
- su -l postgres \
- -c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
- /usr/lib/postgresql-@SLOT@/bin/pg_ctl \
- stop ${WAIT_FOR_STOP} -t ${NICE_TIMEOUT} -s -D ${DATA_DIR} \
- -m smart"
- retval=$?
-
- if [ $retval -eq 0 ] ; then
- eend $retval
- return $retval
- fi
-
- ewarn "Shutting down the server gracefully failed."
- ewarn "Probably because some clients did not disconnect within ${NICE_TIMEOUT} seconds."
- else
- ewarn "NICE_QUIT disabled."
- ewarn "You really should have it enabled."
- fi
+ local retries=SIGTERM/$((${NICE_TIMEOUT}*1000))
if [ "${RUDE_QUIT}" != "NO" ] ; then
- ewarn "RUDE_QUIT enabled."
- ewarn "Going to shutdown the server anyway."
-
- su -l postgres \
- -c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
- /usr/lib/postgresql-@SLOT@/bin/pg_ctl \
- stop ${WAIT_FOR_STOP} -t ${RUDE_TIMEOUT} -s -D ${DATA_DIR} \
- -m fast"
- retval=$?
-
- if [ $retval -eq 0 ] ; then
- eend $retval
- return $retval
- fi
-
- eerror "Failed to shutdown server."
- else
- ewarn "RUDE_QUIT disabled."
+ einfo "RUDE_QUIT enabled."
+ retries="${retries}/SIGINT/$((${RUDE_TIMEOUT}*1000))"
fi
-
if [ "${FORCE_QUIT}" = "YES" ] ; then
- ewarn "FORCE_QUIT enabled."
- ewarn "Forcing server to shutdown."
- ewarn "A recover-run will be executed on the next startup."
-
- su -l postgres \
- -c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
- /usr/lib/postgresql-@SLOT@/bin/pg_ctl \
- stop ${WAIT_FOR_STOP} -t ${FORCE_TIMEOUT} -s -D ${DATA_DIR} \
- -m immediate"
-
- retval=$?
-
- if [ $retval -eq 0 ] ; then
- ewarn "Server forced down."
- eend $retval
- return $retval
- fi
-
- eerror "Forced shutdown failed!!!"
- eerror "Something is wrong with your system."
- eerror "Please take care of it manually."
- eerror "Unable to stop server."
- eend $retval
- return $retval
- else
- ewarn "FORCE_QUIT disabled."
- eerror "Unable to shutdown server."
- eend 1
- return 1
+ einfo "FORCE_QUIT enabled."
+ ewarn "A recover-run might be executed on next startup."
+ retries="${retries}/SIGQUIT/$((${FORCE_TIMEOUT}*1000))"
fi
-}
-reload() {
- ebegin "Reloading PostgreSQL configuration"
- su -l postgres \
- -c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
- /usr/lib/postgresql-@SLOT@/bin/pg_ctl \
- reload -s -D ${DATA_DIR}"
- eend $?
+ start-stop-daemon --stop \
+ --exec /usr/lib/postgresql-@SLOT@/bin/postgres \
+ --retry ${retries} \
+ --pidfile ${DATA_DIR}/postmaster.pid
+
+ eend
}
-status() {
+reload() {
ebegin "Reloading PostgreSQL configuration"
- su -l postgres \
- -c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
- /usr/lib/postgresql-@SLOT@/bin/pg_ctl \
- status -D ${DATA_DIR}"
+ kill -HUP $(head -n1 ${DATA_DIR}/postmaster.pid)
eend $?
}