#!/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: $POSTGRES_DATA_DIR/postmaster.pid # Source function library . /etc/rc.d/init.d/functions # Get network config . /etc/sysconfig/network # Get service config if [ -f /etc/sysconfig/postgresql ] ; then . /etc/sysconfig/postgresql else ALLOW_TCP_CONNECTIONS=no ALLOW_USE_SSL=no POSTGRES_DATA_DIR=/var/lib/pgsql POSTGRES_LOG=/var/log/pgsql fi # Check that networking is up. if is_no "${NETWORKING}"; then msg_network_down PostgreSQL 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 else # it's stopped. rm -f /var/lock/subsys/postgresql fi # 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 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 else # 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 TMPDIR=/tmp su postgres -c "LD_LIBRARY_PATH=/usr/lib \ initdb --pgdata=$POSTGRES_DATA_DIR \ -L /usr/share/postgresql" 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')"; 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 else deltext; fail exit 1 fi fi touch /var/lock/subsys/postgresql ;; 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 ;; status) PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")"; TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl status $PGSQL_CMDLINE" ;; restart) $0 stop $0 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 else deltext; fail echo -e "\n$out"; exit 1 fi ;; *) msg_usage "$0 {start|stop|status|restart|reload}" exit 1 esac exit 0