cvsdist 027e0bd
#!/bin/sh
cvsdist 6f2c3a9
# postgresql	This is the init script for starting up the PostgreSQL
cvsdist 6f2c3a9
#		server
cvsdist 6f2c3a9
#
3c38c70
# chkconfig: - 64 36
cvsdist 6f2c3a9
# description: Starts and stops the PostgreSQL backend daemon that handles \
cvsdist 6f2c3a9
#	       all database requests.
cvsdist 6f2c3a9
# processname: postmaster
cvsdist 6f2c3a9
# pidfile: /var/run/postmaster.pid
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# Version 6.5.3-2 Lamar Owen
cvsdist 6f2c3a9
# Added code to determine if PGDATA exists, whether it is current version
cvsdist 6f2c3a9
#     or not, and initdb if no PGDATA (initdb will not overwrite a database).
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# Version 7.0 Lamar Owen
cvsdist 6f2c3a9
# Added logging code
cvsdist 6f2c3a9
# Changed PGDATA.
cvsdist 6f2c3a9
cvsdist 06e8d17
# Version 7.0.2 Trond Eivind Glomsrd <teg@redhat.com>
cvsdist 6f2c3a9
# use functions, add conditional restart
cvsdist 6f2c3a9
          
cvsdist 6f2c3a9
# Version 7.0.3 Lamar Owen <lamar@postgresql.org>
cvsdist 6f2c3a9
# Check for the existence of functions before blindly using them 
cvsdist 6f2c3a9
# in particular -- check for success () and failure () before using.
cvsdist 6f2c3a9
# More Cross-distribution support -- PGVERSION variable, and docdir checks.
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# Version 7.1 Release Candidate Lamar Owen <lamar@postgresql.org>
cvsdist 6f2c3a9
# initdb parameters have changed.
cvsdist 6f2c3a9
cvsdist 06e8d17
# Version 7.1.2 Trond Eivind Glomsrd <teg@redhat.com>
cvsdist 6f2c3a9
# Specify shell for su
cvsdist 6f2c3a9
# Handle stop better - kill unwanted output, make it wait until the database is ready
cvsdist 6f2c3a9
# Handle locales slightly differently - always using "C" isn't a valid option 
cvsdist 6f2c3a9
# Kill output from database initialization 
cvsdist 6f2c3a9
# Mark messages for translation
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# Version 7.1.2-2.PGDG Lamar Owen <lamar.owen@wgcr.org>
cvsdist 6f2c3a9
# sync up.
cvsdist 6f2c3a9
# Karl's fixes for some quoting issues.
cvsdist 6f2c3a9
cvsdist 68d4798
# Version 7.2b2 Lamar Owen <lamar.owen@wgcr.org>
cvsdist 68d4798
# version change.
cvsdist 68d4798
cvsdist 9636c11
# Version 7.2 final.  Lamar Owen <lamar.owen@wgcr.org>
cvsdist 9636c11
# reload from Peter E.
cvsdist 9636c11
# Eliminate the pidof postmaster test in stop -- we're using pg_ctl so we don't need pidof.
cvsdist 9636c11
# Tested the $? return for the stop script -- it does in fact propagate.
cvsdist 9636c11
# TODO: multiple postmasters.
cvsdist 9636c11
cvsdist aba59f5
# Version 7.3 Lamar Owen <lamar.owen@ramifordistat.net>
cvsdist 06e8d17
# Multiple postmasters, courtesy Karl DeBisschop
cvsdist 06e8d17
cvsdist aba59f5
# Version 7.4 Lamar Owen.
cvsdist aba59f5
cvsdist aba59f5
# Version 7.4.3 Tom Lane <tgl@sss.pgh.pa.us>
cvsdist aba59f5
# Support condstop for uninstall
cvsdist aba59f5
# Minor other changes suggested by Fernando Nasser.
cvsdist aba59f5
3fd417a
# Version 7.4.5 Tom Lane <tgl@sss.pgh.pa.us>
3fd417a
# Rewrite to start postmaster directly, rather than via pg_ctl; this avoids
3fd417a
# fooling the postmaster's stale-lockfile check by having too many
3fd417a
# postgres-owned processes laying about.
3fd417a
cvsdist c205ec8
26b954a
# PGVERSION is the full package version, e.g., 7.4.7
26b954a
# Note: the specfile ordinarily updates this during install
26b954a
PGVERSION=xxxx
26b954a
# PGMAJORVERSION is major version, e.g., 7.4 (this should match PG_VERSION)
26b954a
PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# Source function library.
cvsdist 6f2c3a9
INITD=/etc/rc.d/init.d
cvsdist 6f2c3a9
. $INITD/functions
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# Get function listing for cross-distribution logic.
cvsdist 6f2c3a9
TYPESET=`typeset -f|grep "declare"`
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# Get config.
cvsdist 6f2c3a9
. /etc/sysconfig/network
cvsdist 6f2c3a9
cvsdist 06e8d17
# Find the name of the script
cvsdist 06e8d17
NAME=`basename $0`
cvsdist c8cd9cb
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
cvsdist c8cd9cb
then
cvsdist c8cd9cb
	NAME=${NAME:3}
cvsdist c8cd9cb
fi
cvsdist c8cd9cb
3fd417a
# For SELinux we need to use 'runuser' not 'su'
3fd417a
if [ -x /sbin/runuser ]
3fd417a
then
3fd417a
    SU=runuser
3fd417a
else
3fd417a
    SU=su
3fd417a
fi
3fd417a
cvsdist 06e8d17
cvsdist aba59f5
# Set defaults for configuration variables
cvsdist aba59f5
PGENGINE=/usr/bin
cvsdist 06e8d17
PGPORT=5432
6e96691
PGDATA=/var/lib/pgsql
6e96691
if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base/template1" ]
cvsdist 06e8d17
then
cvsdist 06e8d17
	echo "Using old-style directory structure"
cvsdist 06e8d17
else
6e96691
	PGDATA=/var/lib/pgsql/data
cvsdist 06e8d17
fi
6e96691
PGLOG=/var/lib/pgsql/pgstartup.log
cvsdist 06e8d17
cvsdist 06e8d17
# Override defaults from /etc/sysconfig/pgsql if file is present
cvsdist 06e8d17
[ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME}
6e96691
cvsdist 06e8d17
export PGDATA
cvsdist 06e8d17
export PGPORT
cvsdist 06e8d17
cvsdist 6f2c3a9
# Check that networking is up.
cvsdist 6f2c3a9
# Pretty much need it for postmaster.
cvsdist 6f2c3a9
[ "${NETWORKING}" = "no" ] && exit 0
cvsdist 6f2c3a9
6e96691
[ -f "$PGENGINE/postmaster" ] || exit 1
6e96691
6e96691
script_result=0
cvsdist 6f2c3a9
cvsdist 6f2c3a9
start(){
cvsdist 06e8d17
	PSQL_START=$"Starting ${NAME} service: "
cvsdist 6f2c3a9
6e96691
	# Make sure startup-time log file is valid
6e96691
	if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
6e96691
	then
6e96691
		touch "$PGLOG" || exit 1
6e96691
		chown postgres:postgres "$PGLOG"
6e96691
		chmod go-rwx "$PGLOG"
6e96691
		[ -x /usr/bin/chcon ] && /usr/bin/chcon -u system_u -r object_r -t postgresql_log_t "$PGLOG"
6e96691
	fi
6e96691
cvsdist 6f2c3a9
	# Check for the PGDATA structure
6e96691
	if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
cvsdist 6f2c3a9
	then
cvsdist 6f2c3a9
	# Check version of existing PGDATA
cvsdist 6f2c3a9
26b954a
		if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ]
cvsdist 6f2c3a9
		then
cvsdist 6f2c3a9
			SYSDOCDIR="(Your System's documentation directory)"
6e96691
			if [ -d "/usr/doc/postgresql-$PGVERSION" ]
cvsdist 6f2c3a9
			then
cvsdist 6f2c3a9
				SYSDOCDIR=/usr/doc
cvsdist 6f2c3a9
			fi
6e96691
			if [ -d "/usr/share/doc/postgresql-$PGVERSION" ]
cvsdist 6f2c3a9
			then
cvsdist 6f2c3a9
				SYSDOCDIR=/usr/share/doc
cvsdist 6f2c3a9
			fi
6e96691
			if [ -d "/usr/doc/packages/postgresql-$PGVERSION" ]
cvsdist 6f2c3a9
			then
cvsdist 6f2c3a9
				SYSDOCDIR=/usr/doc/packages
cvsdist 6f2c3a9
			fi
6e96691
			if [ -d "/usr/share/doc/packages/postgresql-$PGVERSION" ]
cvsdist 6f2c3a9
			then
cvsdist 6f2c3a9
				SYSDOCDIR=/usr/share/doc/packages
cvsdist 6f2c3a9
			fi
cvsdist 6f2c3a9
			echo
26b954a
			echo $"An old version of the database format was found."
26b954a
			echo $"You need to upgrade the data format before using PostgreSQL."
26b954a
			echo $"See $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information."
cvsdist 6f2c3a9
			exit 1
cvsdist 6f2c3a9
		fi
cvsdist 6f2c3a9
cvsdist 6f2c3a9
	# No existing PGDATA! Initdb it.
cvsdist 6f2c3a9
cvsdist 6f2c3a9
	else
6e96691
		echo -n $"Initializing database: "
6e96691
		if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]
cvsdist 6f2c3a9
		then
6e96691
			mkdir -p "$PGDATA" || exit 1
6e96691
			chown postgres:postgres "$PGDATA"
6e96691
			chmod go-rwx "$PGDATA"
cvsdist 6f2c3a9
		fi
6e96691
		# Clean up SELinux tagging for PGDATA
6e96691
		[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
cvsdist 6f2c3a9
		# Initialize the database
6e96691
		$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident sameuser'" >> "$PGLOG" 2>&1 < /dev/null
6e96691
		# Create directory for postmaster log
6e96691
		mkdir "$PGDATA/pg_log"
6e96691
		chown postgres:postgres "$PGDATA/pg_log"
6e96691
		chmod go-rwx "$PGDATA/pg_log"
6e96691
6e96691
		[ -f "$PGDATA/PG_VERSION" ] && echo_success
6e96691
		[ ! -f "$PGDATA/PG_VERSION" ] && echo_failure
cvsdist 6f2c3a9
		echo
cvsdist 6f2c3a9
 	fi
cvsdist 6f2c3a9
3fd417a
	echo -n "$PSQL_START"
6e96691
	$SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
6e96691
	sleep 2
6e96691
	pid=`pidof -s "$PGENGINE/postmaster"`
6e96691
	if [ $pid ] && [ -f "$PGDATA/postmaster.pid" ]
cvsdist 6f2c3a9
	then
3fd417a
		success "$PSQL_START"
3fd417a
		touch /var/lock/subsys/${NAME}
6e96691
		head -n 1 "$PGDATA/postmaster.pid" > "/var/run/postmaster.${PGPORT}.pid"
3fd417a
		echo
cvsdist 6f2c3a9
	else
3fd417a
		failure "$PSQL_START"
3fd417a
		echo
6e96691
		script_result=1
cvsdist 6f2c3a9
	fi
cvsdist 6f2c3a9
}
cvsdist 6f2c3a9
cvsdist 6f2c3a9
stop(){
cvsdist 06e8d17
	echo -n $"Stopping ${NAME} service: "
6e96691
	$SU -l postgres -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null
cvsdist 06e8d17
	ret=$? 
cvsdist 06e8d17
	if [ $ret -eq 0 ]
cvsdist 6f2c3a9
	then
cvsdist 06e8d17
		echo_success
cvsdist 6f2c3a9
	else
cvsdist 06e8d17
		echo_failure
6e96691
		script_result=1
cvsdist 6f2c3a9
	fi
cvsdist 6f2c3a9
	echo
6e96691
	rm -f "/var/run/postmaster.${PGPORT}.pid"
6e96691
	rm -f "/var/lock/subsys/${NAME}"
cvsdist 6f2c3a9
}
cvsdist 6f2c3a9
cvsdist 6f2c3a9
restart(){
cvsdist 6f2c3a9
    stop
cvsdist 6f2c3a9
    start
cvsdist 6f2c3a9
}
cvsdist 6f2c3a9
cvsdist 6f2c3a9
condrestart(){
cvsdist 06e8d17
    [ -e /var/lock/subsys/${NAME} ] && restart
cvsdist 6f2c3a9
}
cvsdist 6f2c3a9
cvsdist aba59f5
condstop(){
cvsdist aba59f5
    [ -e /var/lock/subsys/${NAME} ] && stop
cvsdist aba59f5
}
cvsdist aba59f5
cvsdist 9636c11
reload(){
6e96691
    $SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
cvsdist 9636c11
}
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# This script is slightly unusual in that the name of the daemon (postmaster)
cvsdist 6f2c3a9
# is not the same as the name of the subsystem (postgresql)
cvsdist 6f2c3a9
cvsdist 6f2c3a9
# See how we were called.
cvsdist 6f2c3a9
case "$1" in
cvsdist 6f2c3a9
  start)
cvsdist 6f2c3a9
	start
cvsdist 6f2c3a9
	;;
cvsdist 6f2c3a9
  stop)
cvsdist 6f2c3a9
	stop
cvsdist 6f2c3a9
	;;
cvsdist 6f2c3a9
  status)
cvsdist 6f2c3a9
	status postmaster
6e96691
	script_result=$?
cvsdist 6f2c3a9
	;;
cvsdist 6f2c3a9
  restart)
cvsdist 6f2c3a9
	restart
cvsdist 6f2c3a9
	;;
cvsdist 6f2c3a9
  condrestart)
cvsdist 6f2c3a9
	condrestart
cvsdist 6f2c3a9
	;;
cvsdist aba59f5
  condstop)
cvsdist aba59f5
	condstop
cvsdist aba59f5
	;;
cvsdist 9636c11
  reload|force-reload)
cvsdist 9636c11
	reload
cvsdist 9636c11
	;;
cvsdist 6f2c3a9
  *)
cvsdist aba59f5
	echo $"Usage: $0 {start|stop|status|restart|condrestart|condstop|reload|force-reload}"
cvsdist 6f2c3a9
	exit 1
cvsdist 6f2c3a9
esac
cvsdist 6f2c3a9
6e96691
exit $script_result