File: //etc/init.d/postgresql
#!/bin/sh
#
# postgresql This is the init script for starting up the PostgreSQL
# server.
#
# chkconfig: - 64 36
# description: PostgreSQL database server.
# processname: postmaster
# pidfile="/usr/local/apps/pgsql16/var/run/postgres.pid"
### BEGIN INIT INFO
# Provides: PostgreSQL Server
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and Stop PostgreSQL Server
### END INIT INFO
# PGMAJORVERSION is major version, e.g., 9.3 (this should match PG_VERSION)
PGVERSION=16
PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
PGPREVMAJORVERSION=16.2
# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`
# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
then
NAME=${NAME:3}
fi
SU=su
# Define variable for locale parameter:
LOCALEPARAMETER=$2
# Set defaults for configuration variables
PGENGINE=/usr/local/apps/pgsql16/bin
PGPORT=5432
PGDATA=/var/lib/pgsql/data
PGLOG=/usr/local/apps/pgsql16/log/pgstartup.log
# Log file for pg_upgrade
PGUPLOG=/usr/local/apps/pgsql16/log/pgupgrade.log
lockfile="/var/lock/subsys/${NAME}"
pidfile="/usr/local/apps/pgsql16/var/run/postgres.pid"
export PGDATA
export PGPORT
[ -f "$PGENGINE/postmaster" ] || exit 1
start(){
[ -x "$PGENGINE/postmaster" ] || exit 5
PSQL_START=$"Starting ${NAME} : "
# 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"
fi
# Check for the PGDATA structure
if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
then
# Check version of existing PGDATA
if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ]
then
echo
echo $"An old version of the database format was found."
echo $"You need to upgrade the data format before using PostgreSQL."
fi
else
# No existing PGDATA! Warn the user to initdb it.
echo "$PGDATA is missing. Use \"service $NAME initdb\" to initialize the cluster first."
exit 1
fi
if [ ! -f $pidfile ]; then
echo -n "$PSQL_START"
$SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
sleep 2
pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null`
if [ "x$pid" != x ]
then
echo "[ OK ]"
touch "$lockfile"
echo $pid > "$pidfile"
else
echo "[ fail ]"
fi
else
echo "$NAME already running"
fi
return 0
}
stop(){
if [ -f $pidfile ]; then
echo -n $"Stopping ${NAME} service: "
$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 "[ OK ]"
rm -f "$pidfile"
rm -f "$lockfile"
else
echo "[ fail ]"
fi
else
echo "$NAME stopped"
fi
}
restart(){
stop
start
}
status() {
if [ ! -f $pidfile ] ; then
echo "$prog stopped"
else
PID=`cat $pidfile`
echo "$prog (pid $PID) is running"
fi
}
initdb(){
# If the locale name is specified just after the initdb parameter, use it:
if [ -z $LOCALEPARAMETER ]
then
LOCALE=`echo $LANG`
else
LOCALE=`echo $LOCALEPARAMETER`
fi
LOCALESTRING="--locale=$LOCALE"
if [ -f "$PGDATA/PG_VERSION" ]
then
echo "Data directory is not empty!"
exit
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
# 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"
fi
# Initialize the database
$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident' $LOCALESTRING" >> "$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"
[ -f "$PGDATA/PG_VERSION" ] && echo "[ OK ]"
[ ! -f "$PGDATA/PG_VERSION" ] && echo "[ fail ]"
fi
}
condrestart(){
[ -e "$lockfile" ] && restart || :
}
reload(){
$SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}
promote(){
$SU -l postgres -c "$PGENGINE/pg_ctl promote -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
initdb)
initdb
;;
promote)
promote
;;
condrestart|try-restart)
condrestart
;;
reload)
reload
;;
force-reload)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|upgrade|condrestart|try-restart|reload|force-reload|initdb|promote}"
exit 2
esac