postgresql/postgresql.init

245 lines
5.2 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2010-02-22 18:16:56 +00:00
#
# postgresql This is the init script for starting up the PostgreSQL
2010-02-22 18:16:56 +00:00
# server.
#
# chkconfig: - 64 36
2010-02-22 18:16:56 +00:00
# description: PostgreSQL database server.
# processname: postmaster
2010-02-22 18:16:56 +00:00
# pidfile: /var/run/postmaster.PORT.pid
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
# PGVERSION is the full package version, e.g., 8.4.0
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
2009-08-18 02:58:55 +00:00
# PGMAJORVERSION is major version, e.g., 8.4 (this should match PG_VERSION)
2005-02-21 21:44:36 +00:00
PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
# Source function library.
. /etc/rc.d/init.d/functions
2010-02-22 18:16:56 +00:00
# Get network config.
. /etc/sysconfig/network
# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
then
NAME=${NAME:3}
fi
# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
SU=runuser
else
SU=su
fi
# Set defaults for configuration variables
PGENGINE=/usr/bin
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
# Value to set as postmaster process's oom_adj
PG_OOM_ADJ=-17
# 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
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
start(){
2010-02-22 18:16:56 +00:00
[ -x "$PGENGINE/postmaster" ] || exit 5
PSQL_START=$"Starting ${NAME} service: "
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"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
2005-01-19 05:02:11 +00:00
fi
# Check for the PGDATA structure
2005-01-19 05:02:11 +00:00
if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
then
2010-02-22 18:16:56 +00:00
# Check version of existing PGDATA
2005-02-21 21:44:36 +00:00
if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ]
then
SYSDOCDIR="(Your System's documentation directory)"
2005-01-19 05:02:11 +00:00
if [ -d "/usr/doc/postgresql-$PGVERSION" ]
then
SYSDOCDIR=/usr/doc
fi
2005-01-19 05:02:11 +00:00
if [ -d "/usr/share/doc/postgresql-$PGVERSION" ]
then
SYSDOCDIR=/usr/share/doc
fi
2005-01-19 05:02:11 +00:00
if [ -d "/usr/doc/packages/postgresql-$PGVERSION" ]
then
SYSDOCDIR=/usr/doc/packages
fi
2005-01-19 05:02:11 +00:00
if [ -d "/usr/share/doc/packages/postgresql-$PGVERSION" ]
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."
exit 1
fi
else
2010-02-22 18:16:56 +00:00
# No existing PGDATA! Warn the user to initdb it.
echo
echo "$PGDATA is missing. Use \"service postgresql initdb\" to initialize the cluster first."
echo_failure
echo
exit 1
fi
echo -n "$PSQL_START"
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 ]
then
success "$PSQL_START"
2010-02-22 18:16:56 +00:00
touch "$lockfile"
echo $pid > "$pidfile"
echo
else
failure "$PSQL_START"
echo
2005-01-19 05:02:11 +00:00
script_result=1
fi
}
stop(){
echo -n $"Stopping ${NAME} service: "
2010-02-22 18:16:56 +00:00
if [ -e "$lockfile" ]
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
echo_success
2010-02-22 18:16:56 +00:00
rm -f "$pidfile"
rm -f "$lockfile"
else
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
fi
echo
}
restart(){
stop
start
}
condrestart(){
2010-02-22 18:16:56 +00:00
[ -e "$lockfile" ] && restart || :
}
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
}
initdb(){
if [ -f "$PGDATA/PG_VERSION" ]
then
2008-11-02 18:23:29 +00:00
echo -n "Data directory is not empty!"
echo_failure
2008-11-02 18:23:29 +00:00
echo
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"
# Make sure the startup-time log file is OK, too
if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
then
touch "$PGLOG" || exit 1
chown postgres:postgres "$PGLOG"
chmod go-rwx "$PGLOG"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
fi
# 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
# 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
2008-11-02 18:23:29 +00:00
echo
fi
}
# 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=$?
;;
restart)
restart
;;
2010-02-22 18:16:56 +00:00
condrestart|try-restart)
condrestart
;;
2010-02-22 18:16:56 +00:00
reload)
reload
;;
2010-02-22 18:16:56 +00:00
force-reload)
restart
;;
initdb)
initdb
;;
*)
2010-02-22 18:16:56 +00:00
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|initdb}"
exit 2
esac
2005-01-19 05:02:11 +00:00
exit $script_result