3 # postgresql This is the init script for starting up the PostgreSQL
8 # description: Starts and stops the PostgreSQL backend daemon that handles \
9 # all database requests.
11 # processname: postmaster
13 # Source function library
14 . /etc/rc.d/init.d/functions
17 . /etc/sysconfig/network
20 if [ -f /etc/sysconfig/postgresql ] ; then
21 . /etc/sysconfig/postgresql
23 echo "Error: /etc/sysconfig/postgresql not found"
24 echo " PostgreSQL can't be run."
28 # Check that networking is up.
29 if is_no "${NETWORKING}"; then
30 msg_network_down PostgreSQL
35 # check for postgresql status
41 # PG_STATUS = running | not running
42 # PG_PID = pid of postmaster process
47 status=`/usr/bin/pg_ctl -D $1 status`
49 if echo "$status" | grep -q 'pg_ctl: postmaster or postgres is not running'; then
50 PG_STATUS="not running"
51 elif echo "$status" | grep -q 'pg_ctl: postmaster is running'; then
53 PG_PID=`echo "$status" | sed 's/^pg_ctl:.*pid: \([0-9]\+\).*/\1/' | head -1`
58 # start postgresql and display appropriate messages
64 # PG_STARTED = 1 if instance has started up; please note, that function
65 # will not set to other value
68 msg_starting "PostgreSQL $1"
70 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null"
73 if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then
83 # any db cluster as command line argument?
88 DB_CLUSTERS="$PG_DB_CLUSTERS"
91 # See how we were called.
92 # Every action is performed for all given (all configured by default)
97 for pgdir in $DB_CLUSTERS; do
99 if [ "$PG_STATUS" = "running" ]; then
100 # pg_ctl status can misinform us about postgresql status
101 # so let's check if postmaster is really alive
102 if ps ax | grep -v grep | grep -q "$PG_PID"; then
103 msg_already_running "PostgreSQL $pgdir"
105 # pg_ctl has misinformed us about postgresql status;
106 # remove pid file and run postgresql
107 msg_not_running "PostgreSQL $pgdir"
108 rm -f $pgdir/postmaster.pid
115 # one of instances has started, so create the lock
116 [ $PG_STARTED -eq 1 ] && touch /var/lock/subsys/postgresql
119 for pgdir in $DB_CLUSTERS; do
121 if [ "$PG_STATUS" = "not running" ]; then
122 msg_not_running "PostgreSQL $pgdir"
124 msg_stopping "PostgreSQL $pgdir"
127 # is postgresql really alive?
128 if ps ax | grep -v grep | grep -q "$PG_PID"; then
129 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
131 if [ "$PG_STATUS" != "not running" ]; then
137 # postgresql is not really alive; pg_ctl misinformed us
142 rm -f $pgdir/postmaster.pid
147 for pgdir in $DB_CLUSTERS; do
148 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
152 $0 stop "$DB_CLUSTERS"
153 $0 start "$DB_CLUSTERS"
156 for pgdir in $DB_CLUSTERS; do
158 if [ "$PG_STATUS" = "not running" ]; then
159 msg_not_running "PostgreSQL $pgdir"
161 msg_reloading "PostgreSQL $pgdir"
164 # is postgresql really alive?
165 if ps ax | grep -v grep | grep -q "$PG_PID"; then
166 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null"
169 if [ "$PG_STATUS" = "running" ]; then
175 # postgresql died and pg_ctl has misinformed us about
176 # the status; remove pid file and start it again
178 rm -f $pgdir/postmaster.pid
185 msg_usage "$0 {start|stop|status|restart|reload}"