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 nls "Error: %s not found" /etc/sysconfig/postgresql
24 nls " PostgreSQL can't be run."
28 if [ ! "$PG_DB_CLUSTERS" ]; then
29 nls "Error: PG_DB_CLUSTERS not found or is empty"
30 nls " PostgreSQL can't be run."
34 # Check that networking is up.
35 if is_yes "${NETWORKING}"; then
36 if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
37 msg_network_down PostgreSQL
46 # any db cluster as command line argument?
49 # perform action for specified clusters only
52 DB_CLUSTERS="$PG_DB_CLUSTERS"
60 # check for postgresql status
66 # PG_STATUS = running | not running
67 # PG_PID = pid of postmaster process
72 status=`/usr/bin/pg_ctl -D $1 status`
74 if echo "$status" | grep -q 'pg_ctl: postmaster or postgres is not running'; then
75 PG_STATUS="not running"
76 elif echo "$status" | grep -q 'pg_ctl: postmaster is running'; then
78 PG_PID=`echo "$status" | sed 's/^pg_ctl:.*pid: \([0-9]\+\).*/\1/' | head -1`
83 # start postgresql and display appropriate messages
89 msg_starting "PostgreSQL $1"
91 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null"
94 if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then
102 # check for running postgresql instances; if any instance is running then
103 # create subsys lock file
106 # check for every defined db cluster in sysconfig file
107 for pgdir in $PG_DB_CLUSTERS; do
109 if [ "$PG_STATUS" = "running" ]; then
110 touch /var/lock/subsys/postgresql
114 rm -f /var/lock/subsys/postgresql
118 # End of useful functions.
122 # See how we were called.
123 # Every action is performed for all given (all configured by default)
127 for pgdir in $DB_CLUSTERS; do
129 if [ "$PG_STATUS" = "running" ]; then
130 # pg_ctl status can misinform us about postgresql status
131 # so let's check if postmaster is really alive
132 if ps ax | grep -v grep | grep -q "$PG_PID"; then
133 msg_already_running "PostgreSQL $pgdir"
135 # pg_ctl has misinformed us about postgresql status;
136 # remove pid file and run postgresql
137 msg_not_running "PostgreSQL $pgdir"
138 rm -f $pgdir/postmaster.pid
148 for pgdir in $DB_CLUSTERS; do
150 if [ "$PG_STATUS" = "not running" ]; then
151 msg_not_running "PostgreSQL $pgdir"
153 msg_stopping "PostgreSQL $pgdir"
155 # is postgresql really alive?
156 if ps ax | grep -v grep | grep -q "$PG_PID"; then
157 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
159 if [ "$PG_STATUS" != "not running" ]; then
165 # postgresql is not really alive; pg_ctl misinformed us
169 rm -f $pgdir/postmaster.pid
175 for pgdir in $DB_CLUSTERS; do
176 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
180 $0 stop "$DB_CLUSTERS"
181 $0 start "$DB_CLUSTERS"
184 for pgdir in $DB_CLUSTERS; do
186 if [ "$PG_STATUS" = "not running" ]; then
187 msg_not_running "PostgreSQL $pgdir"
189 msg_reloading "PostgreSQL $pgdir"
191 # is postgresql really alive?
192 if ps ax | grep -v grep | grep -q "$PG_PID"; then
193 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null"
195 if [ "$PG_STATUS" = "running" ]; then
201 # postgresql died and pg_ctl has misinformed us about
202 # the status; remove pid file and start it again
204 rm -f $pgdir/postmaster.pid
212 nls "Note: this is only simple init action for convenience."
213 nls "If you want some non-standard options, consider using initdb(1)."
215 for pgdir in $DB_CLUSTERS; do
216 if [ -f $pgdir/PG_VERSION ]; then
217 echo $(nls "Skipping existing cluster %s" "$pgdir")
219 echo $(nls "Initializing cluster %s" "$pgdir")
222 chown postgres.postgres $pgdir
223 TMPDIR=/tmp su - postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir"
226 echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
229 msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"