]> git.pld-linux.org Git - packages/postgresql.git/blame - postgresql.init
- unification
[packages/postgresql.git] / postgresql.init
CommitLineData
c35cc882 1#!/bin/sh
2#
d29ea5e3 3# postgresql This is the init script for starting up the PostgreSQL
4# server
5#
4f7ddd24 6# chkconfig: 345 84 25
c35cc882 7#
8# description: Starts and stops the PostgreSQL backend daemon that handles \
24852107 9# all database requests.
c35cc882 10#
11# processname: postmaster
c35cc882 12
c35cc882 13# Source function library
d29ea5e3 14. /etc/rc.d/init.d/functions
15
c35cc882 16# Get network config
d29ea5e3 17. /etc/sysconfig/network
18
c35cc882 19# Get service config
dafcea45 20if [ -f /etc/sysconfig/postgresql ]; then
24852107 21 . /etc/sysconfig/postgresql
22else
dd94d10a 23 nls "Error: %s not found" /etc/sysconfig/postgresql
b5cf88e8 24 nls " PostgreSQL can't be run."
24852107 25 exit 1
03af0381 26fi
27
dafcea45 28if [ ! "$PG_DB_CLUSTERS" ]; then
b5cf88e8 29 nls "Error: PG_DB_CLUSTERS not found or is empty"
30 nls " PostgreSQL can't be run."
dafcea45 31 exit 1
32fi
33
c35cc882 34# Check that networking is up.
b5cf88e8 35if is_yes "${NETWORKING}"; then
4b821775 36 if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
b5cf88e8 37 msg_network_down PostgreSQL
38 exit 1
39 fi
40else
41 exit 0
d584f4fd 42fi
d29ea5e3 43
93ace391 44action="$1"
45
46# any db cluster as command line argument?
47if [ $# -gt 1 ]; then
48 shift
49 # perform action for specified clusters only
50 DB_CLUSTERS="$@"
51else
52 DB_CLUSTERS="$PG_DB_CLUSTERS"
53fi
54
55#
56# Useful functions.
57#
58
ee1e3deb 59#
60# check for postgresql status
61#
62# arguments:
63# $1 - db cluster
64#
65# sets variables:
66# PG_STATUS = running | not running
67# PG_PID = pid of postmaster process
68#
293d606e 69pgstatus() {
24852107 70 PG_STATUS="unknown"
71 PG_PID="unknown"
72 status=`/usr/bin/pg_ctl -D $1 status`
15e46b8a 73
24852107 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
77 PG_STATUS="running"
78 PG_PID=`echo "$status" | sed 's/^pg_ctl:.*pid: \([0-9]\+\).*/\1/' | head -1`
79 fi
80}
81
ee1e3deb 82#
83# start postgresql and display appropriate messages
293d606e 84#
ee1e3deb 85# arguments:
86# $1 - db cluster
87#
88pgstart() {
93ace391 89 msg_starting "PostgreSQL $1"
90 busy
91 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null"
fc99439e 92 sleep 1
93ace391 93 pgstatus "$1"
94 if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then
b5cf88e8 95 ok
93ace391 96 else
b5cf88e8 97 fail
93ace391 98 fi
ee1e3deb 99}
100
93ace391 101#
102# check for running postgresql instances; if any instance is running then
103# create subsys lock file
104#
105pgsubsys() {
106 # check for every defined db cluster in sysconfig file
107 for pgdir in $PG_DB_CLUSTERS; do
108 pgstatus "$pgdir"
109 if [ "$PG_STATUS" = "running" ]; then
110 touch /var/lock/subsys/postgresql
111 return
112 fi
113 done
114 rm -f /var/lock/subsys/postgresql
115}
24852107 116
93ace391 117#
118# End of useful functions.
119#
15e46b8a 120
b5cf88e8 121
d29ea5e3 122# See how we were called.
293d606e 123# Every action is performed for all given (all configured by default)
124# db clusters.
24852107 125case "$action" in
b5cf88e8 126 start)
127 for pgdir in $DB_CLUSTERS; do
128 pgstatus "$pgdir"
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"
24852107 134 else
b5cf88e8 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
93ace391 139 pgstart "$pgdir"
2a514bb5 140 fi
b5cf88e8 141 else
142 pgstart "$pgdir"
143 fi
144 done
145 pgsubsys
d29ea5e3 146 ;;
b5cf88e8 147 stop)
148 for pgdir in $DB_CLUSTERS; do
149 pgstatus "$pgdir"
150 if [ "$PG_STATUS" = "not running" ]; then
151 msg_not_running "PostgreSQL $pgdir"
152 else
153 msg_stopping "PostgreSQL $pgdir"
154 busy
93ace391 155 # is postgresql really alive?
b5cf88e8 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"
158 pgstatus "$pgdir"
159 if [ "$PG_STATUS" != "not running" ]; then
160 fail
24852107 161 else
b5cf88e8 162 ok
24852107 163 fi
b5cf88e8 164 else
165 # postgresql is not really alive; pg_ctl misinformed us
166 # about the status
167 died
24852107 168 fi
b5cf88e8 169 rm -f $pgdir/postmaster.pid
170 fi
171 done
172 pgsubsys
d29ea5e3 173 ;;
b5cf88e8 174 status)
175 for pgdir in $DB_CLUSTERS; do
176 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
93ace391 177 done
d29ea5e3 178 ;;
b5cf88e8 179 restart)
180 $0 stop "$DB_CLUSTERS"
181 $0 start "$DB_CLUSTERS"
962e37bd 182 ;;
9c6c5600 183 reload|force-reload)
b5cf88e8 184 for pgdir in $DB_CLUSTERS; do
185 pgstatus "$pgdir"
186 if [ "$PG_STATUS" = "not running" ]; then
187 msg_not_running "PostgreSQL $pgdir"
188 else
189 msg_reloading "PostgreSQL $pgdir"
190 busy
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"
24852107 194 pgstatus "$pgdir"
b5cf88e8 195 if [ "$PG_STATUS" = "running" ]; then
9c6c5600 196 ok
24852107 197 else
9c6c5600 198 fail
24852107 199 fi
4239f689 200 else
b5cf88e8 201 # postgresql died and pg_ctl has misinformed us about
202 # the status; remove pid file and start it again
203 deltext; died
204 rm -f $pgdir/postmaster.pid
205 pgstart "$pgdir"
4239f689 206 fi
b5cf88e8 207 fi
208 done
209 pgsubsys
4239f689 210 ;;
b5cf88e8 211 init)
212 nls "Note: this is only simple init action for convenience."
213 nls "If you want some non-standard options, consider using initdb(1)."
214 echo
215 for pgdir in $DB_CLUSTERS; do
216 if [ -f $pgdir/PG_VERSION ]; then
217 echo $(nls "Skipping existing cluster %s" "$pgdir")
218 else
219 echo $(nls "Initializing cluster %s" "$pgdir")
220 mkdir -p $pgdir
221 chmod 700 $pgdir
222 chown postgres.postgres $pgdir
bf4934a3 223 TMPDIR=/tmp su - postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir"
b5cf88e8 224 fi
225 done
226 echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
227 ;;
228 *)
9c6c5600 229 msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"
230 exit 3
d29ea5e3 231esac
232
233exit 0
This page took 0.212462 seconds and 4 git commands to generate.