diff --git a/postgresql-common-1/debian/init.d-functions b/postgresql-common-1/debian/init.d-functions new file mode 100644 index 0000000000000000000000000000000000000000..c0c3841571a7cab441826123a1b59957b3f24376 --- /dev/null +++ b/postgresql-common-1/debian/init.d-functions @@ -0,0 +1,113 @@ +# This file contains common functionality for all postgresql server +# package init.d scripts. It is usually included by +# /etc/init.d/postgresqlX.Y. + +# Make sure the log directory and file exist with the correct ownership +# needs correct $VERSION +assert_logfile() { + if [ -z "$VERSION" ]; then + echo "Error: assert_logfile called without proper \$VERSION" >&2 + exit 1 + fi + + LOGFILE=${POSTGRES_LOG:-/var/log/postgresql/postgresql-$VERSION.log} + LOGDIR="`dirname \"$LOGFILE\"`" + + [ -d "$LOGDIR" ] || install -d -m 770 -o postgres -g postgres "$LOGDIR" + + if [ ! -e "$LOGFILE" ]; then + touch "$LOGFILE" + chown postgres.postgres "$LOGFILE" + chmod 640 "$LOGFILE" + fi +} + +# Get $PGDATA path, $PG_VERSION, and $PG_CTL path from given cluster name ($1) and +# check that the # cluster data exists. +prepare_cluster() { + PGDATA=/etc/postgresql/$1/pgdata + if [ ! -d $PGDATA ]; then + echo "Error: symbolic link $PGDATA to data directory does not exist, exiting" >&2 + exit 1 + fi + + if [ ! -f $PGDATA/PG_VERSION ]; then + echo "Error: $PGDATA does not seem to contain a PostgreSQL cluster (PG_VERSION is missing)" >&2 + exit 1 + fi + PG_VERSION=$(< $PGDATA/PG_VERSION) + + PG_CTL=/usr/lib/postgresql/$PG_VERSION/bin/pg_ctl +} + +# start given cluster +start() { + echo -n "Starting PostgreSQL database $1 server: postmaster" + prepare_cluster $1 + assert_logfile + + ERRMSG=$(/sbin/start-stop-daemon --pidfile $PGDATA/postmaster.pid \ + --oknodo --chuid postgres --exec $PG_CTL --start -- \ + -D $PGDATA -l $LOGFILE -s start 2>&1) + + if [ $? != 0 ]; then + echo "(FAILED)" + [ "$ERRMSG" ] && echo -e "\nERROR: $ERRMSG" >&2 || true + exit 1 + fi + + [ "$ERRMSG" ] && echo -n "($ERRMSG)" >&2 || true + echo "." +} + +# stop given cluster (i. e. major version) +stop() { + echo -n "Stopping PostgreSQL database $1 server: postmaster" + prepare_cluster $1 + + start-stop-daemon -c postgres --start --exec $PG_CTL -- -D "$PGDATA" stop -s -w -m fast + + # try harder if "fast" mode does not work + if [ -f "$PGDATA/postmaster.pid" ]; then + echo -n "(does not shutdown gracefully, now stopping immediately)" + start-stop-daemon -c postgres --start --exec $PG_CTL -- -D "$PGDATA" stop -s -w -m immediate + fi + + # if that still not helps, use the big hammer + if [ -f "$PGDATA/postmaster.pid" ]; then + echo -n "(does not shutdown, killing the process)" + PID=`head -n 1 "$PGDATA/postmaster.pid"` + if [ "$PID" ]; then + kill -9 "$PID" || true + rm -f "$PGDATA/postmaster.pid" + fi + fi + echo "." +} + +# reload given cluster +reload() { + echo -n "Reloading PostgreSQL database $1 server: postmaster" + prepare_cluster $1 + + ERRMSG=$(start-stop-daemon --chuid postgres --start \ + --exec $PG_CTL -- -D $PGDATA -s reload) + + if [ $? != 0 ]; then + echo "(FAILED)" + [ "$ERRMSG" ] && echo "ERROR: $ERRMSG" >&2 || true + exit 1 + fi + + [ "$ERRMSG" ] && echo -n " ($ERRMSG)" >&2 || true + echo "." +} + +# print status for given cluster +status() { + prepare_cluster $1 + + echo "-- Cluster $1 --" + start-stop-daemon --chuid postgres --start --exec $PG_CTL -- -D $PGDATA status +} + diff --git a/postgresql-common-1/debian/postgresql-common.install b/postgresql-common-1/debian/postgresql-common.install index b6afdb1745e61502ea8f4e37ab34cf72ae06bf77..1934cf3dc854a24ca8a4e0bf1128d4d94387b7c6 100644 --- a/postgresql-common-1/debian/postgresql-common.install +++ b/postgresql-common-1/debian/postgresql-common.install @@ -1,2 +1,3 @@ cluster_ports etc/postgresql user_clusters etc/postgresql +debian/init.d-functions /etc/postgresql/