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