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
15 # Source function library
16 . /etc/rc.d/init.d/functions
19 . /etc/sysconfig/network
22 if [ -f /etc/sysconfig/postgresql ]; then
23 . /etc/sysconfig/postgresql
25 nls "Error: %s not found" /etc/sysconfig/postgresql
26 nls " PostgreSQL can't be run."
30 if [ ! "$PG_DB_CLUSTERS" ]; then
31 nls "Error: PG_DB_CLUSTERS not found or is empty"
32 nls " PostgreSQL can't be run."
36 # Check that networking is up.
37 if is_yes "${NETWORKING}"; then
38 if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
39 msg_network_down PostgreSQL
48 # any db cluster as command line argument?
51 # perform action for specified clusters only
54 DB_CLUSTERS="$PG_DB_CLUSTERS"
62 # check for postgresql status
68 # PG_STATUS = running | not running
69 # PG_PID = pid of postmaster process
74 output=$(LC_ALL=C TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $1 status")
77 if [ $status -eq 1 ]; then
78 PG_STATUS="not running"
79 elif [ $status -eq 0 ]; then
81 # or maybe grab it from postmaster.pid file?
82 PG_PID=$(echo "$output" | awk '/PID: / { match($0, "PID: [0-9]+"); print substr($0,RSTART+5,RLENGTH-5) }')
87 # start postgresql and display appropriate messages
93 msg_starting "PostgreSQL $1"
94 daemon --user postgres /usr/bin/pg_ctl -s -w -D $1 start
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.
118 for pgdir in $DB_CLUSTERS; do
120 if [ "$PG_STATUS" = "running" ]; then
121 # pg_ctl status can misinform us about postgresql status
122 # so let's check if postmaster is really alive
123 if ps -p "$PG_PID" >/dev/null; then
124 msg_already_running "PostgreSQL $pgdir"
126 # pg_ctl has misinformed us about postgresql status;
127 # remove pid file and run postgresql
128 msg_not_running "PostgreSQL $pgdir"
129 rm -f $pgdir/postmaster.pid
140 for pgdir in $DB_CLUSTERS; do
142 if [ "$PG_STATUS" = "not running" ]; then
143 msg_not_running "PostgreSQL $pgdir"
145 msg_stopping "PostgreSQL $pgdir"
147 # is postgresql really alive?
148 if ps -p "$PG_PID" >/dev/null; then
149 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
151 if [ "$PG_STATUS" != "not running" ]; then
157 # postgresql is not really alive; pg_ctl misinformed us
161 rm -f $pgdir/postmaster.pid
167 # See how we were called.
168 # Every action is performed for all given (all configured by default)
178 for pgdir in $DB_CLUSTERS; do
180 if [ "$PG_STATUS" = "running" ]; then
181 show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID"
182 progress "$PG_STATUS"
184 show "PostgreSQL cluster %s" "$pgdir"
185 progress "$PG_STATUS" "$CFAIL"
195 for pgdir in $DB_CLUSTERS; do
197 if [ "$PG_STATUS" = "not running" ]; then
198 msg_not_running "PostgreSQL $pgdir"
200 msg_reloading "PostgreSQL $pgdir"
202 # is postgresql really alive?
203 if ps -p "$PG_PID" >/dev/null; then
204 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null"
206 if [ "$PG_STATUS" = "running" ]; then
212 # postgresql died and pg_ctl has misinformed us about
213 # the status; remove pid file and start it again
215 rm -f $pgdir/postmaster.pid
223 nls "Note: this is only simple init action for convenience."
224 nls "If you want some non-standard options, consider using initdb(1)."
226 for pgdir in $DB_CLUSTERS; do
227 if [ -f $pgdir/PG_VERSION ]; then
228 echo $(nls "Skipping existing cluster %s" "$pgdir")
230 echo $(nls "Initializing cluster %s" "$pgdir")
233 chown postgres:postgres $pgdir
234 TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir"
237 echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
240 msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"