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 if [ ! "$PG_DB_CLUSTERS" ]; then
29 echo "Error: PG_DB_CLUSTERS not found or is empty"
30 echo " PostgreSQL can't be run."
34 # Check that networking is up.
35 if is_no "${NETWORKING}"; then
36 msg_network_down PostgreSQL
42 # any db cluster as command line argument?
45 # perform action for specified clusters only
48 DB_CLUSTERS="$PG_DB_CLUSTERS"
56 # check for postgresql status
62 # PG_STATUS = running | not running
63 # PG_PID = pid of postmaster process
68 status=`/usr/bin/pg_ctl -D $1 status`
70 if echo "$status" | grep -q 'pg_ctl: postmaster or postgres is not running'; then
71 PG_STATUS="not running"
72 elif echo "$status" | grep -q 'pg_ctl: postmaster is running'; then
74 PG_PID=`echo "$status" | sed 's/^pg_ctl:.*pid: \([0-9]\+\).*/\1/' | head -1`
79 # start postgresql and display appropriate messages
85 msg_starting "PostgreSQL $1"
87 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null"
90 if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then
98 # check for running postgresql instances; if any instance is running then
99 # create subsys lock file
102 # check for every defined db cluster in sysconfig file
103 for pgdir in $PG_DB_CLUSTERS; do
105 if [ "$PG_STATUS" = "running" ]; then
106 touch /var/lock/subsys/postgresql
110 rm -f /var/lock/subsys/postgresql
114 # End of useful functions.
117 # See how we were called.
118 # Every action is performed for all given (all configured by default)
122 for pgdir in $DB_CLUSTERS; do
124 if [ "$PG_STATUS" = "running" ]; then
125 # pg_ctl status can misinform us about postgresql status
126 # so let's check if postmaster is really alive
127 if ps ax | grep -v grep | grep -q "$PG_PID"; then
128 msg_already_running "PostgreSQL $pgdir"
130 # pg_ctl has misinformed us about postgresql status;
131 # remove pid file and run postgresql
132 msg_not_running "PostgreSQL $pgdir"
133 rm -f $pgdir/postmaster.pid
143 for pgdir in $DB_CLUSTERS; do
145 if [ "$PG_STATUS" = "not running" ]; then
146 msg_not_running "PostgreSQL $pgdir"
148 msg_stopping "PostgreSQL $pgdir"
151 # is postgresql really alive?
152 if ps ax | grep -v grep | grep -q "$PG_PID"; then
153 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
155 if [ "$PG_STATUS" != "not running" ]; then
161 # postgresql is not really alive; pg_ctl misinformed us
166 rm -f $pgdir/postmaster.pid
172 for pgdir in $DB_CLUSTERS; do
173 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
177 $0 stop "$DB_CLUSTERS"
178 $0 start "$DB_CLUSTERS"
181 for pgdir in $DB_CLUSTERS; do
183 if [ "$PG_STATUS" = "not running" ]; then
184 msg_not_running "PostgreSQL $pgdir"
186 msg_reloading "PostgreSQL $pgdir"
189 # is postgresql really alive?
190 if ps ax | grep -v grep | grep -q "$PG_PID"; then
191 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null"
194 if [ "$PG_STATUS" = "running" ]; then
200 # postgresql died and pg_ctl has misinformed us about
201 # the status; remove pid file and start it again
203 rm -f $pgdir/postmaster.pid
211 msg_usage "$0 {start|stop|status|restart|reload}"