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