-#! /bin/sh
+#!/bin/sh
+#
# postgresql This is the init script for starting up the PostgreSQL
# server
#
-# chkconfig: 345 85 15
-# description: Starts and stops the PostgreSQL backend daemon that handles \
-# all database requests.
-# processname: postmaster
-# pidfile: /var/run/postmaster.pid
-#
+# chkconfig: 345 85 15
+#
+# description: Starts and stops the PostgreSQL backend daemon that handles \
+# all database requests.
+#
+# processname: postmaster
+# pidfile: /var/run/postmaster.pid
-# Source function library.
+# Source function library
. /etc/rc.d/init.d/functions
-# Get config.
+# Get network config
. /etc/sysconfig/network
-# Check that networking is up.
-# Pretty much need it for postmaster.
-[ ${NETWORKING} = "no" ] && exit 0
-
-[ -f /usr/bin/postmaster ] || exit 0
-
-[ -f /tmp/.s.PGSQL.5432 ] || rm -f /tmp/.s.PGSQL.5432
+# Get service config
+if [ -f /etc/sysconfig/postgresql ] ; then
+ . /etc/sysconfig/postgresql
+else
+ ALLOW_TCP_CONNECTIONS=no
+ POSTGRES_DATA_DIR=/var/lib/psql
+ POSTGRES_LOG=/var/log/pgsql
+fi
-# This script is slightly unusual in that the name of the daemon (postmaster)
-# is not the same as the name of the subsystem (postgresql)
+# Check that networking is up.
+if is_no "${NETWORKING}"; then
+ msg_Network_Down Postgresql
+ exit 1
+fi
# See how we were called.
case "$1" in
start)
- show Starting postgresql service
- busy
- su postgres -c '/usr/bin/postmaster -S -D/var/lib/pgsql'
- sleep 1
- pid=`pidof postmaster`
+ # Check if the service is already running?
+ if [ -f /var/lock/subsys/postgresql ]; then
+ msg_Already_Running PostgreSQL
+ else
+ # Sanity check
+ [ -f /tmp/.s.PGSQL.5432 ] || rm -f /tmp/.s.PGSQL.5432
+
+ # Check for the PGDATA structure
+ if [ -f $POSTGRES_DATA_DIR/PG_VERSION ] && [ -d $POSTGRES_DATA_DIR/base/template1 ]; then
+ # Check version of existing PGDATA
+ if [ `cat $POSTGRES_DATA_DIR/PG_VERSION` != '7.0' ]; then
+ show "Databases created under incompatibile version. Migrate them first."; fail
+ exit 1
+ fi
+ else
+ # Create sample database
+ if [ ! -d $POSTGRES_DATA_DIR ]; then
+ mkdir -p $POSTGRES_DATA_DIR
+ chown postgres.postgres $POSTGRES_DATA_DIR
+ fi
+ su postgres -c "LD_LIBRARY_PATH=/usr/lib \
+ initdb --pgdata=$POSTGRES_DATA_DIR \
+ --pglib=/usr/lib/pgsql"
+ fi
+
+ msg_starting PostgreSQL
+ busy
+ PGSQL_CMDLINE="$([ "$ALLOW_TCP_CONNECTIONS" = yes ] && echo "-i") \
+ $([ -n "$MAX_NUM_BACKENDS" ] && echo "-N $MAX_NUM_BACKENDS") \
+ $([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
+ $([ "$POSTGRES_QUIET". = yes. ] && echo '-o -Q') \
+ $([ "$POSTGRES_DISABLE_FSYNC". = yes. ] && echo '-o -F')";
+ su postgres -c "/usr/bin/postmaster $PGSQL_CMDLINE >>$POSTGRES_LOG 2>&1 &"
+ out=`status postmaster`
+ if [ $? -eq 0 ]; then
+ deltext; ok
+ else
+ deltext; fail
+ exit 1
+ fi
+ fi
touch /var/lock/subsys/postgresql
- echo $pid > /var/run/postmaster.pid
- deltext; ok
;;
stop)
- show Stopping postgresql service
+ if [ ! -f /var/lock/subsys/postgresql ]; then
+ msg_Not_Running PostgreSQL
+ exit 0
+ fi
+ msg_stopping postgreSQL
killproc postmaster
- sleep 2
+ out=`status postmaster`
+ if [ $? -eq 0 ]; then
+ deltext; fail
+ echo -e "\n$out";
+ exit 1
+ fi
+
rm -f /var/run/postmaster.pid
rm -f /var/lock/subsys/postgresql
;;
status)
status postmaster
;;
- restart)
+ restart|reload)
$0 stop
$0 start
;;
*)
- echo "Usage: postgresql {start|stop|status|restart}"
+ msg_Usage "$0 {start|stop|status|restart|reload}"
exit 1
esac