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
21 # Preconfigure some variables:
22 PG_INITDB_ENCODING=UNICODE
25 if [ -f /etc/sysconfig/postgresql ]; then
26 . /etc/sysconfig/postgresql
28 nls "Error: %s not found" /etc/sysconfig/postgresql
29 nls " PostgreSQL can't be run."
33 if [ ! "$PG_DB_CLUSTERS" ]; then
34 nls "Error: PG_DB_CLUSTERS not found or is empty"
35 nls " PostgreSQL can't be run."
39 # Check that networking is up.
40 if is_yes "${NETWORKING}"; then
41 if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
42 msg_network_down PostgreSQL
51 # any db cluster as command line argument?
54 # perform action for specified clusters only
57 DB_CLUSTERS="$PG_DB_CLUSTERS"
65 # check for postgresql status
71 # PG_STATUS = running | not running
72 # PG_PID = pid of postmaster process
77 output=`TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $1 status"`
80 if [ $status -eq 1 ]; then
81 PG_STATUS="not running"
82 elif [ $status -eq 0 ]; then
84 # or maybe grab it from postmaster.pid file?
85 PG_PID=`echo "$output" | sed 's/^pg_ctl:.*PID: \([0-9]\+\).*/\1/' | head -1`
90 # start postgresql and display appropriate messages
96 msg_starting "PostgreSQL $1"
97 daemon --user postgres /usr/bin/pg_ctl -s -w -D $1 start
101 # check for running postgresql instances; if any instance is running then
102 # create subsys lock file
105 # check for every defined db cluster in sysconfig file
106 for pgdir in $PG_DB_CLUSTERS; do
108 if [ "$PG_STATUS" = "running" ]; then
109 touch /var/lock/subsys/postgresql
113 rm -f /var/lock/subsys/postgresql
117 # End of useful functions.
121 # See how we were called.
122 # Every action is performed for all given (all configured by default)
126 for pgdir in $DB_CLUSTERS; do
128 if [ "$PG_STATUS" = "running" ]; then
129 # pg_ctl status can misinform us about postgresql status
130 # so let's check if postmaster is really alive
131 if ps ax | grep -v grep | grep -q "$PG_PID"; then
132 msg_already_running "PostgreSQL $pgdir"
134 # pg_ctl has misinformed us about postgresql status;
135 # remove pid file and run postgresql
136 msg_not_running "PostgreSQL $pgdir"
137 rm -f $pgdir/postmaster.pid
147 for pgdir in $DB_CLUSTERS; do
149 if [ "$PG_STATUS" = "not running" ]; then
150 msg_not_running "PostgreSQL $pgdir"
152 msg_stopping "PostgreSQL $pgdir"
154 # is postgresql really alive?
155 if ps ax | grep -v grep | grep -q "$PG_PID"; then
156 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
158 if [ "$PG_STATUS" != "not running" ]; then
164 # postgresql is not really alive; pg_ctl misinformed us
168 rm -f $pgdir/postmaster.pid
174 for pgdir in $DB_CLUSTERS; do
175 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
179 $0 stop "$DB_CLUSTERS"
180 $0 start "$DB_CLUSTERS"
183 for pgdir in $DB_CLUSTERS; do
185 if [ "$PG_STATUS" = "not running" ]; then
186 msg_not_running "PostgreSQL $pgdir"
188 msg_reloading "PostgreSQL $pgdir"
190 # is postgresql really alive?
191 if ps ax | grep -v grep | grep -q "$PG_PID"; then
192 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 nls "Note: this is only simple init action for convenience."
212 nls "If you want some non-standard options, consider using initdb(1)."
214 for pgdir in $DB_CLUSTERS; do
215 if [ -f $pgdir/PG_VERSION ]; then
216 echo $(nls "Skipping existing cluster %s" "$pgdir")
218 echo $(nls "Initializing cluster %s" "$pgdir")
221 chown postgres:postgres $pgdir
222 if [ "x${PG_INITDB_ENCODING}x" != "xx" ]; then
223 TMPENC="-E ${PG_INITDB_ENCODING}"
227 TMPDIR=/tmp su postgres -s /bin/sh -c "initdb ${TMPENC} -D $pgdir"
230 echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
233 msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"