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
63 msg_starting "PostgreSQL $1"
65 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null"
68 if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then
77 # any db cluster as command line argument?
80 POSTGRES_DB_CLUSTERS="$@"
83 # See how we were called.
84 # Every action is performed for all given (default all configured) db clusters.
87 for pgdir in $POSTGRES_DB_CLUSTERS; do
89 if [ "$PG_STATUS" = "running" ]; then
90 # pg_ctl status can misinform us about postgresql status
91 # so let's check if postmaster is really alive
92 if ps ax | grep -v grep | grep -q "$PG_PID"; then
93 msg_already_running "PostgreSQL $pgdir"
95 # pg_ctl has misinformed us about postgresql status;
96 # remove pid file and run postgresql
97 msg_not_running "PostgreSQL $pgdir"
98 rm -f $pgdir/postmaster.pid
107 for pgdir in $POSTGRES_DB_CLUSTERS; do
109 if [ "$PG_STATUS" = "not running" ]; then
110 msg_not_running "PostgreSQL $pgdir"
112 msg_stopping "PostgreSQL $pgdir"
115 # is postgresql really alive?
116 if ps ax | grep -v grep | grep -q "$PG_PID"; then
117 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
119 if [ "$PG_STATUS" != "not running" ]; then
125 # postgresql is not really alive; pg_ctl misinformed us
130 rm -f $pgdir/postmaster.pid
135 for pgdir in $POSTGRES_DB_CLUSTERS; do
136 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
140 $0 stop "$POSTGRES_DB_CLUSTERS"
141 $0 start "$POSTGRES_DB_CLUSTERS"
144 for pgdir in $POSTGRES_DB_CLUSTERS; do
146 if [ "$PG_STATUS" = "not running" ]; then
147 msg_not_running "PostgreSQL $pgdir"
149 msg_reloading "PostgreSQL $pgdir"
152 # is postgresql really alive?
153 if ps ax | grep -v grep | grep -q "$PG_PID"; then
154 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null"
157 if [ "$PG_STATUS" = "running" ]; then
163 # postgresql died and pg_ctl has misinformed us about
164 # the status; remove pid file and start it again
166 rm -f $pgdir/postmaster.pid
173 msg_usage "$0 {start|stop|status|restart|reload}"