# all database requests.
#
# processname: postmaster
-# pidfile: $POSTGRES_DATA_DIR/postmaster.pid
+
+cd /
# Source function library
. /etc/rc.d/init.d/functions
# Get network config
. /etc/sysconfig/network
-# Service config defaults
-ALLOW_TCP_CONNECTIONS=no
-ALLOW_USE_SSL=no
-POSTGRES_DATA_DIR=/var/lib/pgsql
-POSTGRES_LOG=/var/log/pgsql
-ENCODING=UNICODE
-LOCALE=${LANG:-C}
+PG_INIT_LOCALE=C
# Get service config
-if [ -f /etc/sysconfig/postgresql ] ; then
- . /etc/sysconfig/postgresql
+if [ -f /etc/sysconfig/postgresql ]; then
+ . /etc/sysconfig/postgresql
+else
+ nls "Error: %s not found" /etc/sysconfig/postgresql
+ nls " PostgreSQL can't be run."
+ exit 1
fi
-# Check that networking is up.
-if is_no "${NETWORKING}"; then
- msg_network_down PostgreSQL
+if [ ! "$PG_DB_CLUSTERS" ]; then
+ nls "Error: PG_DB_CLUSTERS not found or is empty"
+ nls " PostgreSQL can't be run."
exit 1
fi
-# check if postmaster is realy alive
-if [ -f $POSTGRES_DATA_DIR/postmaster.pid ]; then
- if ps -p `head -1 $POSTGRES_DATA_DIR/postmaster.pid` > /dev/null ; then
- # ok. it's alive
- echo > /dev/null;
- else
- # oh, it has crashed
- rm -f /var/lock/subsys/postgresql
- fi
+# Check that networking is up.
+if is_yes "${NETWORKING}"; then
+ if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
+ msg_network_down PostgreSQL
+ exit 1
+ fi
else
- # it's stopped.
- rm -f /var/lock/subsys/postgresql
+ exit 0
fi
+action="$1"
-# See how we were called.
-case "$1" in
- start)
- # Check if the service is already running?
- if [ -f /var/lock/subsys/postgresql ]; then
- msg_already_running PostgreSQL
- else
- if is_yes "$ALLOW_USE_SSL"; then
- if is_no "$ALLOW_TCP_CONNECTIONS"; then
- nls "You must enable ALLOW_TCP_CONNECTIONS in order to use SSL"
- exit 1
- elif [ ! -f "$POSTGRES_DATA_DIR/server.key" ]; then
- nls "$POSTGRES_DATA_DIR/server.key not found!"
- exit 1
- elif [ ! -f "$POSTGRES_DATA_DIR/server.crt" ]; then
- nls "$POSTGRES_DATA_DIR/server.crt not found!"
- exit 1
- fi
+# any db cluster as command line argument?
+if [ $# -gt 1 ]; then
+ shift
+ # perform action for specified clusters only
+ DB_CLUSTERS="$@"
+else
+ DB_CLUSTERS="$PG_DB_CLUSTERS"
+fi
+
+#
+# Useful functions.
+#
+
+#
+# check for postgresql status
+#
+# arguments:
+# $1 - db cluster
+#
+# sets variables:
+# PG_STATUS = running | not running
+# PG_PID = pid of postmaster process
+#
+pgstatus() {
+ PG_STATUS="unknown"
+ PG_PID="unknown"
+ output=$(LC_ALL=C TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $1 status")
+ status=$?
+
+ if [ $status -eq 3 ]; then
+ PG_STATUS="not running"
+ elif [ $status -eq 0 ]; then
+ PG_STATUS="running"
+ # or maybe grab it from postmaster.pid file?
+ PG_PID=$(echo "$output" | awk '/PID: / { match($0, "PID: [0-9]+"); print substr($0,RSTART+5,RLENGTH-5) }')
+ fi
+}
+
+#
+# start postgresql and display appropriate messages
+#
+# arguments:
+# $1 - db cluster
+#
+pgstart() {
+ is_no "$RC_LOGGING" && fork=--fork
+ msg_starting "PostgreSQL $1"
+ daemon $fork --user postgres /usr/bin/pg_ctl -s -w -D $1 start
+}
+
+#
+# check for running postgresql instances; if any instance is running then
+# create subsys lock file
+#
+pgsubsys() {
+ # check for every defined db cluster in sysconfig file
+ for pgdir in $PG_DB_CLUSTERS; do
+ pgstatus "$pgdir"
+ if [ "$PG_STATUS" = "running" ]; then
+ touch /var/lock/subsys/postgresql
+ return
fi
-
- # Sanity check
- [ -e /tmp/.s.PGSQL.5432 ] || rm -f /tmp/.s.PGSQL.5432
- [ -f /tmp/.s.PGSQL.5432.lock ] || rm -f /tmp/.s.PGSQL.5432.lock
-
- # Check for the PGDATA structure
- if [ -f $POSTGRES_DATA_DIR/PG_VERSION ] && [ -f $POSTGRES_DATA_DIR/global/pg_control ]; then
- # Check version of existing PGDATA
- if [ `cat $POSTGRES_DATA_DIR/PG_VERSION` != '7.2' ]; then
- show "Databases created under incompatibile version. Migrate them first."; fail
- exit 1
- fi
+ done
+ rm -f /var/lock/subsys/postgresql
+}
+
+#
+# End of useful functions.
+#
+
+start() {
+ for pgdir in $DB_CLUSTERS; do
+ pgstatus "$pgdir"
+ if [ "$PG_STATUS" = "running" ]; then
+ # pg_ctl status can misinform us about postgresql status
+ # so let's check if postmaster is really alive
+ if ps -p "$PG_PID" >/dev/null; then
+ msg_already_running "PostgreSQL $pgdir"
+ else
+ # pg_ctl has misinformed us about postgresql status;
+ # remove pid file and run postgresql
+ msg_not_running "PostgreSQL $pgdir"
+ rm -f $pgdir/postmaster.pid
+ pgstart "$pgdir"
+ fi
else
- echo "Postgresql database not initialized. Try \"$0 init\" before start."
- exit 1
+ pgstart "$pgdir"
fi
-
- msg_starting PostgreSQL
- busy
- PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
- $(is_yes "$POSTGRES_QUIET" && echo '-s')";
- POSTMASTER_OPTIONS=" \
- $(is_yes "$POSTGRES_QUIET" && echo '-S') \
- $(is_yes "$POSTGRES_DISABLE_FSYNC" && echo '-F') \
- $(is_yes "$ALLOW_TCP_CONNECTIONS" && echo '-i') \
- $(is_yes "$ALLOW_USE_SSL" && echo '-l') \
- $([ "$BUFFERS" ] && echo \-B $BUFFERS) \
- $([ "$MAXCONN" ] && echo \-N $MAXCONN)";
-
- TMPDIR=/tmp su postgres -c "\
- $(echo "/usr/bin/pg_ctl start -w $PGSQL_CMDLINE -l $POSTGRES_LOG") \
- $([ "$POSTMASTER_OPTIONS". != . ] && echo "-o '$POSTMASTER_OPTIONS'") \
- $(echo "2>&1 >/dev/null")";
-
- out=`status postmaster`
- if [ $? -eq 0 ]; then
- deltext; ok
+ done
+ pgsubsys
+}
+
+stop() {
+ for pgdir in $DB_CLUSTERS; do
+ pgstatus "$pgdir"
+ if [ "$PG_STATUS" = "not running" ]; then
+ msg_not_running "PostgreSQL $pgdir"
else
- deltext; fail
- exit 1
+ msg_stopping "PostgreSQL $pgdir"
+ busy
+ # is postgresql really alive?
+ if ps -p "$PG_PID" >/dev/null; then
+ TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
+ pgstatus "$pgdir"
+ if [ "$PG_STATUS" != "not running" ]; then
+ fail
+ else
+ ok
+ fi
+ else
+ # postgresql is not really alive; pg_ctl misinformed us
+ # about the status
+ died
+ fi
+ rm -f $pgdir/postmaster.pid
fi
- fi
- touch /var/lock/subsys/postgresql
+ done
+ pgsubsys
+}
+
+
+RETVAL=0
+# See how we were called.
+# Every action is performed for all given (all configured by default)
+# db clusters.
+case "$action" in
+ start)
+ start
;;
stop)
-
- if [ ! -f /var/lock/subsys/postgresql ]; then
- msg_not_running PostgreSQL
- exit 0
- fi
- msg_stopping PostgreSQL
- busy
- PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")";
- TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl stop $PGSQL_CMDLINE -m fast 2>&1 >/dev/null"
- out=`status postmaster`
- if [ $? -eq 0 ]; then
- deltext; fail
- echo -e "\n$out";
- exit 1
- else
- deltext; ok
- fi
- rm -f $POSTGRES_DATA_DIR/postmaster.pid
- rm -f /var/lock/subsys/postgresql
+ stop
;;
status)
- PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")";
- TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl status $PGSQL_CMDLINE"
+ for pgdir in $DB_CLUSTERS; do
+ pgstatus "$pgdir"
+ if [ "$PG_STATUS" = "running" ]; then
+ show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID"
+ progress "$PG_STATUS"
+ else
+ show "PostgreSQL cluster %s" "$pgdir"
+ progress "$PG_STATUS" "$CFAIL"
+ fi
+ echo
+ done
;;
restart)
- $0 stop
- $0 start
+ stop
+ start
;;
- reload)
- if [ ! -f /var/lock/subsys/postgresql ]; then
- msg_not_running PostgreSQL
- exit 0
- fi
- msg_reloading PostgreSQL
- busy
-
- PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
- $([ "$POSTGRES_QUIET". = yes. ] && echo '-s')";
- POSTMASTER_OPTIONS=" \
- $([ "$POSTGRES_QUIET". = yes. ] && echo '-S') \
- $([ "$POSTGRES_DISABLE_FSYNC". = yes. ] && echo '-F') \
- $([ "$ALLOW_TCP_CONNECTIONS". = yes. ] && echo '-i')";
-
- TMPDIR=/tmp su postgres -c "\
- $(echo "/usr/bin/pg_ctl restart $PGSQL_CMDLINE -l $POSTGRES_LOG") \
- $([ "$POSTMASTER_OPTIONS". != . ] && echo "-o '$POSTMASTER_OPTIONS'") \
- $(echo "2>&1 >/dev/null")";
-
- out=`status postmaster`
- if [ $? -eq 0 ]; then
- deltext; ok
+ reload|force-reload|try-restart)
+ if [ "$action" = "reload" ]; then
+ # "reload" must not restart service - so let it reload only what's possible
+ pgctlact="reload"
else
- deltext; fail
- echo -e "\n$out";
- exit 1
+ pgctlact="restart"
fi
+ for pgdir in $DB_CLUSTERS; do
+ pgstatus "$pgdir"
+ if [ "$PG_STATUS" = "not running" ]; then
+ msg_not_running "PostgreSQL $pgdir"
+ if [ "$action" != "try-restart" ]; then
+ RETVAL=7
+ fi
+ else
+ msg_reloading "PostgreSQL $pgdir"
+ busy
+ # is postgresql really alive?
+ if ps -p "$PG_PID" >/dev/null; then
+ TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir $pgctlact 2>&1 >/dev/null"
+ pgstatus "$pgdir"
+ if [ "$PG_STATUS" = "running" ]; then
+ ok
+ else
+ fail
+ fi
+ elif [ "$action" != "try-restart" ]; then
+ # postgresql died and pg_ctl has misinformed us about
+ # the status - i.e. service is actually not running
+ RETVAL=7
+ else
+ # postgresql died and pg_ctl has misinformed us about
+ # the status; remove pid file and start it again
+ deltext; died
+ rm -f $pgdir/postmaster.pid
+ pgstart "$pgdir"
+ fi
+ fi
+ done
+ pgsubsys
;;
- init)
- if [ -f "$POSTGRES_DATA_DIR/PG_VERSION" ]; then
- echo "Seems that database is initialized now. Remove by hand $POSTGRES_DATA_DIR"
- echo "before initialize database."
- exit 1
- fi
-
- echo -n "Specify encoding for pgsql database (eg: LATIN2, KOI8) [$ENCODING]: "
- read new_encoding
- ENCODING=${new_encoding:-$ENCODING}
- echo -n "Specify locale for pgsql database (eg: pl_PL, de_DE) [$LOCALE]: "
- read new_locale
- LOCALE=${new_locale:-$LOCALE}
-
- show "Creating initial postgresql database using $ENCODING/$LOCALE encoding/locale"
- started
-
- # Create sample database
- if [ ! -d $POSTGRES_DATA_DIR ]; then
- mkdir -p $POSTGRES_DATA_DIR
- chown postgres.postgres $POSTGRES_DATA_DIR
- chmod 700 $POSTGRES_DATA_DIR
- fi
-
-
- LC_ALL=$LOCALE; export LC_ALL
- TMPDIR=/tmp su - postgres -s /bin/sh -c "LD_LIBRARY_PATH=/usr/lib \
- initdb --pgdata=$POSTGRES_DATA_DIR \
- -L /usr/share/postgresql -E $ENCODING"
-
- echo "REMEMBER to setup password for user \"postgres\"!"
+ init)
+ nls "Note: this is only simple init action for convenience."
+ nls "If you want some non-standard options, consider using initdb(1)."
+ echo
+ for pgdir in $DB_CLUSTERS; do
+ if [ -f $pgdir/PG_VERSION ]; then
+ echo $(nls "Skipping existing cluster %s" "$pgdir")
+ else
+ echo $(nls "Initializing cluster %s" "$pgdir")
+ mkdir -p $pgdir
+ chmod 700 $pgdir
+ chown postgres:postgres $pgdir
+ LC_ALL=$PG_INIT_LOCALE TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE --data-checksums -D $pgdir"
+ fi
+ done
+ echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
;;
*)
- msg_usage "$0 {start|stop|status|restart|reload}"
- exit 1
+ msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"
+ exit 3
esac
-exit 0
+exit $RETVAL