]> git.pld-linux.org Git - packages/postgresql.git/blame - postgresql.init
- rel 4
[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 \
0daa1231 9# all database requests.
c35cc882 10#
11# processname: postmaster
c35cc882 12
bb22748f
AM
13cd /
14
c35cc882 15# Source function library
d29ea5e3 16. /etc/rc.d/init.d/functions
17
c35cc882 18# Get network config
d29ea5e3 19. /etc/sysconfig/network
20
d5258758
AM
21PG_INIT_LOCALE=C
22
c35cc882 23# Get service config
dafcea45 24if [ -f /etc/sysconfig/postgresql ]; then
24852107 25 . /etc/sysconfig/postgresql
26else
dd94d10a 27 nls "Error: %s not found" /etc/sysconfig/postgresql
b5cf88e8 28 nls " PostgreSQL can't be run."
24852107 29 exit 1
03af0381 30fi
31
dafcea45 32if [ ! "$PG_DB_CLUSTERS" ]; then
b5cf88e8 33 nls "Error: PG_DB_CLUSTERS not found or is empty"
34 nls " PostgreSQL can't be run."
dafcea45 35 exit 1
36fi
37
c35cc882 38# Check that networking is up.
b5cf88e8 39if is_yes "${NETWORKING}"; then
4b821775 40 if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
b5cf88e8 41 msg_network_down PostgreSQL
42 exit 1
43 fi
44else
45 exit 0
d584f4fd 46fi
d29ea5e3 47
93ace391 48action="$1"
49
50# any db cluster as command line argument?
51if [ $# -gt 1 ]; then
52 shift
53 # perform action for specified clusters only
54 DB_CLUSTERS="$@"
55else
56 DB_CLUSTERS="$PG_DB_CLUSTERS"
57fi
58
59#
60# Useful functions.
61#
62
ee1e3deb 63#
64# check for postgresql status
65#
66# arguments:
67# $1 - db cluster
68#
69# sets variables:
70# PG_STATUS = running | not running
71# PG_PID = pid of postmaster process
72#
293d606e 73pgstatus() {
24852107 74 PG_STATUS="unknown"
75 PG_PID="unknown"
14caa7e9
ER
76 output=$(LC_ALL=C TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $1 status")
77 status=$?
15e46b8a 78
6091e228 79 if [ $status -eq 1 ]; then
24852107 80 PG_STATUS="not running"
6091e228 81 elif [ $status -eq 0 ]; then
24852107 82 PG_STATUS="running"
6091e228 83 # or maybe grab it from postmaster.pid file?
b3e56078 84 PG_PID=$(echo "$output" | awk '/PID: / { match($0, "PID: [0-9]+"); print substr($0,RSTART+5,RLENGTH-5) }')
24852107 85 fi
86}
87
ee1e3deb 88#
89# start postgresql and display appropriate messages
293d606e 90#
ee1e3deb 91# arguments:
92# $1 - db cluster
93#
94pgstart() {
22afcc50 95 is_no "$RC_LOGGING" && fork=--fork
93ace391 96 msg_starting "PostgreSQL $1"
0e525f4b 97 daemon $fork --user postgres /usr/bin/pg_ctl -s -w -D $1 start
ee1e3deb 98}
99
93ace391 100#
101# check for running postgresql instances; if any instance is running then
102# create subsys lock file
103#
104pgsubsys() {
105 # check for every defined db cluster in sysconfig file
106 for pgdir in $PG_DB_CLUSTERS; do
107 pgstatus "$pgdir"
108 if [ "$PG_STATUS" = "running" ]; then
109 touch /var/lock/subsys/postgresql
110 return
111 fi
112 done
113 rm -f /var/lock/subsys/postgresql
114}
24852107 115
93ace391 116#
117# End of useful functions.
118#
15e46b8a 119
6fd18a8c 120start() {
b5cf88e8 121 for pgdir in $DB_CLUSTERS; do
122 pgstatus "$pgdir"
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
b3e56078 126 if ps -p "$PG_PID" >/dev/null; then
b5cf88e8 127 msg_already_running "PostgreSQL $pgdir"
24852107 128 else
b5cf88e8 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
93ace391 133 pgstart "$pgdir"
2a514bb5 134 fi
b5cf88e8 135 else
136 pgstart "$pgdir"
137 fi
138 done
139 pgsubsys
6fd18a8c
ER
140}
141
142stop() {
b5cf88e8 143 for pgdir in $DB_CLUSTERS; do
144 pgstatus "$pgdir"
145 if [ "$PG_STATUS" = "not running" ]; then
146 msg_not_running "PostgreSQL $pgdir"
147 else
148 msg_stopping "PostgreSQL $pgdir"
149 busy
cec778d4 150 # is postgresql really alive?
b3e56078 151 if ps -p "$PG_PID" >/dev/null; then
b958caa9 152 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
b5cf88e8 153 pgstatus "$pgdir"
154 if [ "$PG_STATUS" != "not running" ]; then
155 fail
24852107 156 else
b5cf88e8 157 ok
24852107 158 fi
b5cf88e8 159 else
160 # postgresql is not really alive; pg_ctl misinformed us
161 # about the status
162 died
24852107 163 fi
b5cf88e8 164 rm -f $pgdir/postmaster.pid
165 fi
166 done
167 pgsubsys
6fd18a8c
ER
168}
169
4d4aed23 170RETVAL=0
6fd18a8c
ER
171# See how we were called.
172# Every action is performed for all given (all configured by default)
173# db clusters.
174case "$action" in
175 start)
176 start
177 ;;
178 stop)
179 stop
d29ea5e3 180 ;;
b5cf88e8 181 status)
182 for pgdir in $DB_CLUSTERS; do
3fd1d6f0 183 pgstatus "$pgdir"
3fd1d6f0 184 if [ "$PG_STATUS" = "running" ]; then
1c07dbe4 185 show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID"
186 progress "$PG_STATUS"
3fd1d6f0 187 else
1c07dbe4 188 show "PostgreSQL cluster %s" "$pgdir"
189 progress "$PG_STATUS" "$CFAIL"
3fd1d6f0 190 fi
1c07dbe4 191 echo
3fd1d6f0 192 done
d29ea5e3 193 ;;
b5cf88e8 194 restart)
6fd18a8c
ER
195 stop
196 start
962e37bd 197 ;;
4d4aed23
JB
198 reload|force-reload|try-restart)
199 if [ "$action" = "reload" ]; then
200 # "reload" must not restart service - so let it reload only what's possible
201 pgctlact="reload"
202 else
203 pgctlact="restart"
204 fi
b5cf88e8 205 for pgdir in $DB_CLUSTERS; do
206 pgstatus "$pgdir"
207 if [ "$PG_STATUS" = "not running" ]; then
208 msg_not_running "PostgreSQL $pgdir"
4d4aed23
JB
209 if [ "$action" != "try-restart" ]; then
210 RETVAL=7
211 fi
b5cf88e8 212 else
213 msg_reloading "PostgreSQL $pgdir"
214 busy
215 # is postgresql really alive?
b3e56078 216 if ps -p "$PG_PID" >/dev/null; then
4d4aed23 217 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir $pgctlact 2>&1 >/dev/null"
24852107 218 pgstatus "$pgdir"
b5cf88e8 219 if [ "$PG_STATUS" = "running" ]; then
9c6c5600 220 ok
24852107 221 else
9c6c5600 222 fail
24852107 223 fi
4d4aed23
JB
224 elif [ "$action" != "try-restart" ]; then
225 # postgresql died and pg_ctl has misinformed us about
226 # the status - i.e. service is actually not running
227 RETVAL=7
4239f689 228 else
b5cf88e8 229 # postgresql died and pg_ctl has misinformed us about
230 # the status; remove pid file and start it again
231 deltext; died
232 rm -f $pgdir/postmaster.pid
233 pgstart "$pgdir"
4239f689 234 fi
b5cf88e8 235 fi
236 done
237 pgsubsys
4239f689 238 ;;
b5cf88e8 239 init)
240 nls "Note: this is only simple init action for convenience."
241 nls "If you want some non-standard options, consider using initdb(1)."
242 echo
243 for pgdir in $DB_CLUSTERS; do
244 if [ -f $pgdir/PG_VERSION ]; then
245 echo $(nls "Skipping existing cluster %s" "$pgdir")
246 else
247 echo $(nls "Initializing cluster %s" "$pgdir")
248 mkdir -p $pgdir
249 chmod 700 $pgdir
c06b99eb 250 chown postgres:postgres $pgdir
d5258758 251 LC_ALL=$PG_INIT_LOCALE TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir"
b5cf88e8 252 fi
253 done
254 echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
255 ;;
256 *)
9c6c5600 257 msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"
258 exit 3
d29ea5e3 259esac
260
4d4aed23 261exit $RETVAL
This page took 0.074812 seconds and 4 git commands to generate.