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
24 if [ -f /etc/sysconfig/postgresql-@pgsqlversion@ ]; then
25 . /etc/sysconfig/postgresql-@pgsqlversion@
27 nls "Error: %s not found" /etc/sysconfig/postgresql-@pgsqlversion@
28 nls " PostgreSQL can't be run."
32 if [ ! "$PG_DB_CLUSTERS" ]; then
33 nls "Error: PG_DB_CLUSTERS not found or is empty"
34 nls " PostgreSQL can't be run."
38 # Check that networking is up.
39 if is_yes "${NETWORKING}"; then
40 if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
41 msg_network_down PostgreSQL
50 # any db cluster as command line argument?
53 # perform action for specified clusters only
56 DB_CLUSTERS="$PG_DB_CLUSTERS"
64 # check for postgresql status
70 # PG_STATUS = running | not running
71 # PG_PID = pid of postmaster process
76 output=$(LC_ALL=C TMPDIR=/tmp su postgres -c "@pgsqlbindir@/pg_ctl -D $1 status")
79 if [ $status -eq 3 ]; then
80 PG_STATUS="not running"
81 elif [ $status -eq 0 ]; then
83 # or maybe grab it from postmaster.pid file?
84 PG_PID=$(echo "$output" | awk '/PID: / { match($0, "PID: [0-9]+"); print substr($0,RSTART+5,RLENGTH-5) }')
89 # start postgresql and display appropriate messages
95 is_no "$RC_LOGGING" && fork=--fork
96 msg_starting "PostgreSQL $1"
97 daemon $fork --user postgres @pgsqlbindir@/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-@pgsqlversion@
113 rm -f /var/lock/subsys/postgresql-@pgsqlversion@
117 # End of useful functions.
121 for pgdir in $DB_CLUSTERS; do
123 if [ "$PG_STATUS" = "running" ]; then
124 # pg_ctl status can misinform us about postgresql status
125 # so let's check if postmaster is really alive
126 if ps -p "$PG_PID" 1>/dev/null 2>&1; then
127 msg_already_running "PostgreSQL $pgdir"
129 # pg_ctl has misinformed us about postgresql status;
130 # remove pid file and run postgresql
131 msg_not_running "PostgreSQL $pgdir"
132 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"
150 # is postgresql really alive?
151 if ps -p "$PG_PID" 1>/dev/null 2>&1; then
152 TMPDIR=/tmp su postgres -c "@pgsqlbindir@/pg_ctl -w -D $pgdir stop -m fast 1>/dev/null 2>&1"
154 if [ "$PG_STATUS" != "not running" ]; then
160 # postgresql is not really alive; pg_ctl misinformed us
164 rm -f $pgdir/postmaster.pid
172 # See how we were called.
173 # Every action is performed for all given (all configured by default)
183 for pgdir in $DB_CLUSTERS; do
185 if [ "$PG_STATUS" = "running" ]; then
186 show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID"
187 progress "$PG_STATUS"
189 show "PostgreSQL cluster %s" "$pgdir"
190 progress "$PG_STATUS" "$CFAIL"
199 reload|force-reload|try-restart)
200 if [ "$action" = "reload" ]; then
201 # "reload" must not restart service - so let it reload only what's possible
206 for pgdir in $DB_CLUSTERS; do
208 if [ "$PG_STATUS" = "not running" ]; then
209 msg_not_running "PostgreSQL $pgdir"
210 if [ "$action" != "try-restart" ]; then
214 msg_reloading "PostgreSQL $pgdir"
216 # is postgresql really alive?
217 if ps -p "$PG_PID" 1>/dev/null 2>&1; then
218 TMPDIR=/tmp su postgres -c "@pgsqlbindir@/pg_ctl -D $pgdir $pgctlact 1>/dev/null 2>&1"
220 if [ "$PG_STATUS" = "running" ]; then
225 elif [ "$action" != "try-restart" ]; then
226 # postgresql died and pg_ctl has misinformed us about
227 # the status - i.e. service is actually not running
230 # postgresql died and pg_ctl has misinformed us about
231 # the status; remove pid file and start it again
233 rm -f $pgdir/postmaster.pid
241 nls "Note: this is only simple init action for convenience."
242 nls "If you want some non-standard options, consider using initdb(1)."
244 for pgdir in $DB_CLUSTERS; do
245 if [ -f $pgdir/PG_VERSION ]; then
246 echo $(nls "Skipping existing cluster %s" "$pgdir")
248 echo $(nls "Initializing cluster %s" "$pgdir")
251 chown postgres:postgres $pgdir
252 LC_ALL=$PG_INIT_LOCALE TMPDIR=/tmp su postgres -s /bin/sh -c "@pgsqlbindir@/initdb -E UNICODE --data-checksums -D $pgdir"
255 echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
258 msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"