2004-09-09 10:47:18 +00:00
|
|
|
#!/bin/sh
|
2010-02-22 18:16:56 +00:00
|
|
|
#
|
2004-09-09 10:37:06 +00:00
|
|
|
# postgresql This is the init script for starting up the PostgreSQL
|
2010-02-22 18:16:56 +00:00
|
|
|
# server.
|
2004-09-09 10:37:06 +00:00
|
|
|
#
|
2004-10-20 17:22:14 +00:00
|
|
|
# chkconfig: - 64 36
|
2010-02-22 18:16:56 +00:00
|
|
|
# description: PostgreSQL database server.
|
2004-09-09 10:37:06 +00:00
|
|
|
# processname: postmaster
|
2010-02-22 18:16:56 +00:00
|
|
|
# pidfile: /var/run/postmaster.PORT.pid
|
2004-09-09 10:39:06 +00:00
|
|
|
|
2010-02-22 18:16:56 +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)
|
2009-08-18 02:58:55 +00:00
|
|
|
|
2010-12-29 00:13:08 +00:00
|
|
|
# PGVERSION is the full package version, e.g., 9.0.2
|
2010-02-22 18:16:56 +00:00
|
|
|
# Note: the specfile inserts the correct value during package build
|
2005-02-21 21:44:36 +00:00
|
|
|
PGVERSION=xxxx
|
2010-12-29 00:13:08 +00:00
|
|
|
# PGMAJORVERSION is major version, e.g., 9.0 (this should match PG_VERSION)
|
2005-02-21 21:44:36 +00:00
|
|
|
PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
|
2010-12-29 00:13:08 +00:00
|
|
|
# PREVMAJORVERSION is the previous major version, e.g., 8.4, for upgrades
|
|
|
|
# Note: the specfile inserts the correct value during package build
|
|
|
|
PREVMAJORVERSION=xxxx
|
|
|
|
# PREVPGENGINE is the directory containing the previous postmaster executable
|
|
|
|
# Note: the specfile inserts the correct value during package build
|
|
|
|
PREVPGENGINE=xxxx
|
|
|
|
# PGDOCDIR is the directory containing the package's documentation
|
|
|
|
# Note: the specfile inserts the correct value during package build
|
|
|
|
PGDOCDIR=xxxx
|
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
|
|
|
|
2010-02-22 18:16:56 +00:00
|
|
|
# Get network config.
|
2004-09-09 10:37:06 +00:00
|
|
|
. /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
|
2009-08-18 02:58:55 +00:00
|
|
|
PGDATA=/var/lib/pgsql/data
|
2005-01-19 05:02:11 +00:00
|
|
|
PGLOG=/var/lib/pgsql/pgstartup.log
|
2010-01-11 18:11:25 +00:00
|
|
|
# Value to set as postmaster process's oom_adj
|
|
|
|
PG_OOM_ADJ=-17
|
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
|
|
|
|
|
2010-02-22 18:16:56 +00:00
|
|
|
lockfile="/var/lock/subsys/${NAME}"
|
|
|
|
pidfile="/var/run/postmaster.${PGPORT}.pid"
|
2005-01-19 05:02:11 +00:00
|
|
|
|
|
|
|
script_result=0
|
2004-09-09 10:37:06 +00:00
|
|
|
|
|
|
|
start(){
|
2010-02-22 18:16:56 +00:00
|
|
|
[ -x "$PGENGINE/postmaster" ] || exit 5
|
|
|
|
|
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
|
2010-02-22 18:16:56 +00:00
|
|
|
touch "$PGLOG" || exit 4
|
2005-01-19 05:02:11 +00:00
|
|
|
chown postgres:postgres "$PGLOG"
|
|
|
|
chmod go-rwx "$PGLOG"
|
2009-09-22 00:45:57 +00:00
|
|
|
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
|
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
|
2010-02-22 18:16:56 +00:00
|
|
|
# Check version of existing PGDATA
|
2010-12-29 00:13:08 +00:00
|
|
|
if [ x`cat "$PGDATA/PG_VERSION"` = x"$PGMAJORVERSION" ]
|
|
|
|
then
|
|
|
|
: A-OK
|
|
|
|
elif [ x`cat "$PGDATA/PG_VERSION"` = x"$PREVMAJORVERSION" ]
|
2004-09-09 10:37:06 +00:00
|
|
|
then
|
|
|
|
echo
|
2005-02-21 21:44:36 +00:00
|
|
|
echo $"An old version of the database format was found."
|
2010-12-29 00:13:08 +00:00
|
|
|
echo $"Use \"service postgresql upgrade\" to upgrade to version $PGMAJORVERSION."
|
|
|
|
echo $"See $PGDOCDIR/README.rpm-dist for more information."
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo
|
|
|
|
echo $"An old version of the database format was found."
|
|
|
|
echo $"You need to dump and reload before using PostgreSQL $PGMAJORVERSION."
|
|
|
|
echo $"See $PGDOCDIR/README.rpm-dist for more information."
|
2004-09-09 10:37:06 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2010-02-22 18:16:56 +00:00
|
|
|
# No existing PGDATA! Warn the user to initdb it.
|
2004-09-09 10:37:06 +00:00
|
|
|
echo
|
2010-12-29 00:13:08 +00:00
|
|
|
echo $"$PGDATA is missing. Use \"service postgresql initdb\" to initialize the cluster first."
|
2006-12-05 03:01:02 +00:00
|
|
|
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"
|
2010-01-11 18:11:25 +00:00
|
|
|
test x"$PG_OOM_ADJ" != x && echo "$PG_OOM_ADJ" > /proc/self/oom_adj
|
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
|
2010-02-22 18:16:56 +00:00
|
|
|
pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null`
|
|
|
|
if [ "x$pid" != x ]
|
2004-09-09 10:37:06 +00:00
|
|
|
then
|
2004-10-05 15:32:24 +00:00
|
|
|
success "$PSQL_START"
|
2010-02-22 18:16:56 +00:00
|
|
|
touch "$lockfile"
|
|
|
|
echo $pid > "$pidfile"
|
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: "
|
2010-02-22 18:16:56 +00:00
|
|
|
if [ -e "$lockfile" ]
|
2004-09-09 10:37:06 +00:00
|
|
|
then
|
2010-02-22 18:16:56 +00:00
|
|
|
$SU -l postgres -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null
|
|
|
|
ret=$?
|
|
|
|
if [ $ret -eq 0 ]
|
|
|
|
then
|
2004-09-09 10:41:55 +00:00
|
|
|
echo_success
|
2010-02-22 18:16:56 +00:00
|
|
|
rm -f "$pidfile"
|
|
|
|
rm -f "$lockfile"
|
|
|
|
else
|
2004-09-09 10:41:55 +00:00
|
|
|
echo_failure
|
2005-01-19 05:02:11 +00:00
|
|
|
script_result=1
|
2010-02-22 18:16:56 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
# not running; per LSB standards this is "ok"
|
|
|
|
echo_success
|
2004-09-09 10:37:06 +00:00
|
|
|
fi
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
restart(){
|
|
|
|
stop
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
|
|
|
condrestart(){
|
2010-02-22 18:16:56 +00:00
|
|
|
[ -e "$lockfile" ] && restart || :
|
2004-09-09 10:47:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-12-29 00:13:08 +00:00
|
|
|
# code shared between initdb and upgrade actions
|
|
|
|
perform_initdb(){
|
2006-12-05 03:01:02 +00:00
|
|
|
if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]
|
|
|
|
then
|
2010-12-29 00:13:08 +00:00
|
|
|
mkdir -p "$PGDATA" || return 1
|
2006-12-05 03:01:02 +00:00
|
|
|
chown postgres:postgres "$PGDATA"
|
|
|
|
chmod go-rwx "$PGDATA"
|
|
|
|
fi
|
|
|
|
# Clean up SELinux tagging for PGDATA
|
|
|
|
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
|
2009-09-22 00:45:57 +00:00
|
|
|
|
|
|
|
# Make sure the startup-time log file is OK, too
|
|
|
|
if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
|
|
|
|
then
|
2010-12-29 00:13:08 +00:00
|
|
|
touch "$PGLOG" || return 1
|
2009-09-22 00:45:57 +00:00
|
|
|
chown postgres:postgres "$PGLOG"
|
|
|
|
chmod go-rwx "$PGLOG"
|
|
|
|
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
|
|
|
|
fi
|
|
|
|
|
2006-12-05 03:01:02 +00:00
|
|
|
# Initialize the database
|
2009-08-18 02:58:55 +00:00
|
|
|
$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" >> "$PGLOG" 2>&1 < /dev/null
|
2009-09-22 00:45:57 +00:00
|
|
|
|
2006-12-05 03:01:02 +00:00
|
|
|
# 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" ]
|
2010-12-29 00:13:08 +00:00
|
|
|
then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
initdb(){
|
|
|
|
if [ -f "$PGDATA/PG_VERSION" ]
|
|
|
|
then
|
|
|
|
echo -n $"Data directory is not empty!"
|
|
|
|
echo_failure
|
|
|
|
echo
|
|
|
|
script_result=1
|
|
|
|
else
|
|
|
|
echo -n $"Initializing database: "
|
|
|
|
if perform_initdb
|
2006-12-05 03:01:02 +00:00
|
|
|
then
|
|
|
|
echo_success
|
|
|
|
else
|
|
|
|
echo_failure
|
|
|
|
script_result=1
|
|
|
|
fi
|
2008-11-02 18:23:29 +00:00
|
|
|
echo
|
2006-12-05 03:01:02 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2010-12-29 00:13:08 +00:00
|
|
|
upgrade(){
|
|
|
|
# must see previous version in PG_VERSION
|
|
|
|
if [ ! -f "$PGDATA/PG_VERSION" -o \
|
|
|
|
x`cat "$PGDATA/PG_VERSION"` != x"$PREVMAJORVERSION" ]
|
|
|
|
then
|
|
|
|
echo
|
|
|
|
echo $"Cannot upgrade because database is not of version $PREVMAJORVERSION."
|
|
|
|
echo_failure
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ ! -x "$PGENGINE/pg_upgrade" ]
|
|
|
|
then
|
|
|
|
echo
|
|
|
|
echo $"Please install the postgresql-upgrade RPM."
|
|
|
|
echo_failure
|
|
|
|
echo
|
|
|
|
exit 5
|
|
|
|
fi
|
|
|
|
# Make sure service is stopped
|
|
|
|
stop
|
|
|
|
|
|
|
|
echo -n $"Upgrading database: "
|
|
|
|
|
|
|
|
# Set up log file for pg_upgrade
|
|
|
|
PGUPLOG=/var/lib/pgsql/pgupgrade.log
|
|
|
|
rm -f "$PGUPLOG"
|
|
|
|
touch "$PGUPLOG" || exit 1
|
|
|
|
chown postgres:postgres "$PGUPLOG"
|
|
|
|
chmod go-rwx "$PGUPLOG"
|
|
|
|
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"
|
|
|
|
|
|
|
|
# Move old DB to PGDATAOLD
|
|
|
|
PGDATAOLD="${PGDATA}-old"
|
|
|
|
rm -rf "$PGDATAOLD"
|
|
|
|
mv "$PGDATA" "$PGDATAOLD" || exit 1
|
|
|
|
|
|
|
|
# Create empty new-format database
|
|
|
|
if perform_initdb
|
|
|
|
then
|
|
|
|
# Do the upgrade
|
|
|
|
$SU -l postgres -c "$PGENGINE/pg_upgrade \
|
|
|
|
'--old-bindir=$PREVPGENGINE' \
|
|
|
|
'--new-bindir=$PGENGINE' \
|
|
|
|
'--old-datadir=$PGDATAOLD' \
|
|
|
|
'--new-datadir=$PGDATA' \
|
|
|
|
--link \
|
|
|
|
'--old-port=$PGPORT' '--new-port=$PGPORT' \
|
|
|
|
--user=postgres" >> "$PGUPLOG" 2>&1 < /dev/null
|
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
# pg_upgrade failed
|
|
|
|
script_result=1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# initdb failed
|
|
|
|
script_result=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $script_result -eq 0 ]
|
|
|
|
then
|
|
|
|
echo_success
|
|
|
|
else
|
|
|
|
# Clean up after failure
|
|
|
|
rm -rf "$PGDATA"
|
|
|
|
mv "$PGDATAOLD" "$PGDATA"
|
|
|
|
|
|
|
|
echo_failure
|
|
|
|
fi
|
|
|
|
echo
|
|
|
|
echo $"See $PGUPLOG for details."
|
|
|
|
}
|
|
|
|
|
2004-09-09 10:37:06 +00:00
|
|
|
# See how we were called.
|
|
|
|
case "$1" in
|
|
|
|
start)
|
|
|
|
start
|
|
|
|
;;
|
|
|
|
stop)
|
|
|
|
stop
|
|
|
|
;;
|
|
|
|
status)
|
2010-10-05 19:19:41 +00:00
|
|
|
status -p "$pidfile" postmaster
|
2005-01-19 05:02:11 +00:00
|
|
|
script_result=$?
|
2004-09-09 10:37:06 +00:00
|
|
|
;;
|
|
|
|
restart)
|
|
|
|
restart
|
|
|
|
;;
|
2010-02-22 18:16:56 +00:00
|
|
|
condrestart|try-restart)
|
2004-09-09 10:37:06 +00:00
|
|
|
condrestart
|
|
|
|
;;
|
2010-02-22 18:16:56 +00:00
|
|
|
reload)
|
2004-09-09 10:39:06 +00:00
|
|
|
reload
|
|
|
|
;;
|
2010-02-22 18:16:56 +00:00
|
|
|
force-reload)
|
|
|
|
restart
|
|
|
|
;;
|
2006-12-05 03:01:02 +00:00
|
|
|
initdb)
|
|
|
|
initdb
|
|
|
|
;;
|
2010-12-29 00:13:08 +00:00
|
|
|
upgrade)
|
|
|
|
upgrade
|
|
|
|
;;
|
2004-09-09 10:37:06 +00:00
|
|
|
*)
|
2010-12-29 00:13:08 +00:00
|
|
|
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|initdb|upgrade}"
|
2010-02-22 18:16:56 +00:00
|
|
|
exit 2
|
2004-09-09 10:37:06 +00:00
|
|
|
esac
|
|
|
|
|
2005-01-19 05:02:11 +00:00
|
|
|
exit $script_result
|