Search Postgresql Archives

Problems Restarting PostgreSQL Daemon

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



  My server is rebooted infrequently, usually after a kernel upgrade and
on very rare occasions when something causes it to hang. After rebooting I
always have serious issues getting postgresql running again, even though the
startup script is part of the boot sequence. Yesterday was one of those
highly unusual hangs, and I cannot restart the service. I'd like to
understand why.

  When I run the Slackware script, '/etc/rc.d/rc.postgresql start' (script
attached), I'm shown a process ID and told the daemon is already running.
For example:

Starting PostgreSQL
15342
PostgreSQL daemon already running

  However, there is no process ID 15342, and no postgres running. I manually
removed /tmp/.s.PGSQL.5432 and its log file. Also -- apparently in error --
the .pid file. Makes no difference.

  Perhaps there's an error in the script that I'm not seeing (I didn't write
it). Regardless, if I learn why there's a problem I can fix the script and
avoid this delay and hassle restarting postgres after the daemon's been shut
down.

TIA,

Rich

--
Richard B. Shepard, Ph.D.               |  Integrity            Credibility
Applied Ecosystem Services, Inc.        |            Innovation
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863
#!/bin/bash

# PostgreSQL startup script for Slackware Linux
# Copyright 2007 Adis Nezirovic <adis _at_ linux.org.ba>
# Licensed under GNU GPL v2

# Do not source this script (since it contains exit() calls)

# Before you can run postgresql you'll need to create the
# database files in /var/lib/pgsql. The following should do
# the trick.
#
#   $ su postgres -c "initdb -D /var/lib/pgsql/data"
#

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

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")
		echo "Starting PostgreSQL"
		touch $LOGFILE
		chown postgres:wheel $LOGFILE
		chmod 0640 $LOGFILE
	
		if [ ! -e $DATADIR/PG_VERSION ]; then
			echo "You should initialize the PostgreSQL database at location $DATADIR"
			exit 6
		fi
	
		if pgrep postgres; 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
				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")
		echo "Shutting down PostgreSQL..."
		pg_ctl stop -l $LOGFILE -D $DATADIR -m smart
	;;

	"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 pgrep postgres; 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: $0 {start|stop|status|restart|reload}"
		exit 1
	;;
esac	

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux