#!/bin/bash # PostgreSQL startup script for Slackware Linux # Copyright 2007 Adis Nezirovic # Licensed under GNU GPL v2 # Do not source this script (since it contains exit() calls) # Adapted to Zenwalk by Pedro Pinto (pnboy~AT~pinguix.com) LOGFILE=/var/log/postgresql DATADIR=/var/lib/pgsql/data POSTGRES=/usr/bin/postgres PIDFILE=postmaster.pid # Return values (according to LSB): # 0 - success # 1 - generic or unspecified error # 2 - invalid or excess argument(s) # 3 - unimplemented feature (e.g. "reload") # 4 - insufficient privilege # 5 - program is not installed # 6 - program is not configured # 7 - program is not running pid="$(pidof postgres 2> /dev/null)" pg_ctl() { CMD="/usr/bin/pg_ctl $@" su - postgres -c "$CMD" } if [ ! -f $POSTGRES ]; then echo "Could not find 'postgres' binary. Maybe PostgreSQL is not installed properly?" exit 5 fi case "$1" in "start") touch $LOGFILE chown postgres:postgres $LOGFILE chmod 0640 $LOGFILE if [ ! -e $DATADIR/PG_VERSION ]; then if [ ! -f /data/PG_VERSION ]; then echo Creating database cluster in $DATADIR... su - postgres -c "initdb -D $DATADIR" fi fi if [ ! -z "${pid}" ]; then echo "PostgreSQL daemon already running" if [ ! -f $DATADIR/$PIDFILE ]; then echo "Warning: Missing pid file $DATADIR/$PIDFILE" fi exit 1 else # remove old socket, if it exists and no daemon is running. if [ ! -f $DATADIR/$PIDFILE ]; then rm -f /tmp/.s.PGSQL.5432 rm -f /tmp/.s.PGSQL.5432.lock echo "Starting PostgreSQL" pg_ctl start -w -l $LOGFILE -D $DATADIR exit 0 else echo "PostgreSQL daemon was not properly shut down" echo "Please remove stale pid file $DATADIR/$PIDFILE" exit 7 fi fi ;; "stop") if [ ! -z "${pid}" ]; then echo "Shutting down PostgreSQL..." pg_ctl stop -l $LOGFILE -D $DATADIR -m smart else echo "PostgreSQL is not running" fi ;; "restart") echo "Restarting PostgreSQL..." pg_ctl restart -l $LOGFILE -D $DATADIR -m smart ;; "reload") echo "Reloading configuration for PostgreSQL..." pg_ctl reload -l $LOGFILE -D $DATADIR -m smart ;; "status") if [ ! -z "${pid}" ]; then echo "PostgreSQL is running" if [ ! -e $DATADIR/$PIDFILE ]; then echo "Warning: Missing pid file $DATADIR/$PIDFILE" fi exit 0 else echo "PostgreSQL is stopped" if [ -e $DATADIR/$PIDFILE ]; then echo "Detected stale pid file $DATADIR/$PIDFILE" fi exit 0 fi ;; *) echo "Usage: $(basename $0) {start|stop|status|restart|reload}" exit 1 ;; esac