]>
Commit | Line | Data |
---|---|---|
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 |
13 | cd / |
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 |
21 | PG_INIT_LOCALE=C |
22 | ||
c35cc882 | 23 | # Get service config |
dafcea45 | 24 | if [ -f /etc/sysconfig/postgresql ]; then |
24852107 | 25 | . /etc/sysconfig/postgresql |
26 | else | |
dd94d10a | 27 | nls "Error: %s not found" /etc/sysconfig/postgresql |
b5cf88e8 | 28 | nls " PostgreSQL can't be run." |
24852107 | 29 | exit 1 |
03af0381 | 30 | fi |
31 | ||
dafcea45 | 32 | if [ ! "$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 |
36 | fi | |
37 | ||
c35cc882 | 38 | # Check that networking is up. |
b5cf88e8 | 39 | if 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 | |
44 | else | |
45 | exit 0 | |
d584f4fd | 46 | fi |
d29ea5e3 | 47 | |
93ace391 | 48 | action="$1" |
49 | ||
50 | # any db cluster as command line argument? | |
51 | if [ $# -gt 1 ]; then | |
52 | shift | |
53 | # perform action for specified clusters only | |
54 | DB_CLUSTERS="$@" | |
55 | else | |
56 | DB_CLUSTERS="$PG_DB_CLUSTERS" | |
57 | fi | |
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 | 73 | pgstatus() { |
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 | # | |
94 | pgstart() { | |
93ace391 | 95 | msg_starting "PostgreSQL $1" |
cec778d4 | 96 | daemon --user postgres /usr/bin/pg_ctl -s -w -D $1 start |
ee1e3deb | 97 | } |
98 | ||
93ace391 | 99 | # |
100 | # check for running postgresql instances; if any instance is running then | |
101 | # create subsys lock file | |
102 | # | |
103 | pgsubsys() { | |
104 | # check for every defined db cluster in sysconfig file | |
105 | for pgdir in $PG_DB_CLUSTERS; do | |
106 | pgstatus "$pgdir" | |
107 | if [ "$PG_STATUS" = "running" ]; then | |
108 | touch /var/lock/subsys/postgresql | |
109 | return | |
110 | fi | |
111 | done | |
112 | rm -f /var/lock/subsys/postgresql | |
113 | } | |
24852107 | 114 | |
93ace391 | 115 | # |
116 | # End of useful functions. | |
117 | # | |
15e46b8a | 118 | |
6fd18a8c | 119 | start() { |
b5cf88e8 | 120 | for pgdir in $DB_CLUSTERS; do |
121 | pgstatus "$pgdir" | |
122 | if [ "$PG_STATUS" = "running" ]; then | |
123 | # pg_ctl status can misinform us about postgresql status | |
124 | # so let's check if postmaster is really alive | |
b3e56078 | 125 | if ps -p "$PG_PID" >/dev/null; then |
b5cf88e8 | 126 | msg_already_running "PostgreSQL $pgdir" |
24852107 | 127 | else |
b5cf88e8 | 128 | # pg_ctl has misinformed us about postgresql status; |
129 | # remove pid file and run postgresql | |
130 | msg_not_running "PostgreSQL $pgdir" | |
131 | rm -f $pgdir/postmaster.pid | |
93ace391 | 132 | pgstart "$pgdir" |
2a514bb5 | 133 | fi |
b5cf88e8 | 134 | else |
135 | pgstart "$pgdir" | |
136 | fi | |
137 | done | |
138 | pgsubsys | |
6fd18a8c ER |
139 | } |
140 | ||
141 | stop() { | |
b5cf88e8 | 142 | for pgdir in $DB_CLUSTERS; do |
143 | pgstatus "$pgdir" | |
144 | if [ "$PG_STATUS" = "not running" ]; then | |
145 | msg_not_running "PostgreSQL $pgdir" | |
146 | else | |
147 | msg_stopping "PostgreSQL $pgdir" | |
148 | busy | |
cec778d4 | 149 | # is postgresql really alive? |
b3e56078 | 150 | if ps -p "$PG_PID" >/dev/null; then |
b958caa9 | 151 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null" |
b5cf88e8 | 152 | pgstatus "$pgdir" |
153 | if [ "$PG_STATUS" != "not running" ]; then | |
154 | fail | |
24852107 | 155 | else |
b5cf88e8 | 156 | ok |
24852107 | 157 | fi |
b5cf88e8 | 158 | else |
159 | # postgresql is not really alive; pg_ctl misinformed us | |
160 | # about the status | |
161 | died | |
24852107 | 162 | fi |
b5cf88e8 | 163 | rm -f $pgdir/postmaster.pid |
164 | fi | |
165 | done | |
166 | pgsubsys | |
6fd18a8c ER |
167 | } |
168 | ||
4d4aed23 | 169 | RETVAL=0 |
6fd18a8c ER |
170 | # See how we were called. |
171 | # Every action is performed for all given (all configured by default) | |
172 | # db clusters. | |
173 | case "$action" in | |
174 | start) | |
175 | start | |
176 | ;; | |
177 | stop) | |
178 | stop | |
d29ea5e3 | 179 | ;; |
b5cf88e8 | 180 | status) |
181 | for pgdir in $DB_CLUSTERS; do | |
3fd1d6f0 | 182 | pgstatus "$pgdir" |
3fd1d6f0 | 183 | if [ "$PG_STATUS" = "running" ]; then |
1c07dbe4 | 184 | show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID" |
185 | progress "$PG_STATUS" | |
3fd1d6f0 | 186 | else |
1c07dbe4 | 187 | show "PostgreSQL cluster %s" "$pgdir" |
188 | progress "$PG_STATUS" "$CFAIL" | |
3fd1d6f0 | 189 | fi |
1c07dbe4 | 190 | echo |
3fd1d6f0 | 191 | done |
d29ea5e3 | 192 | ;; |
b5cf88e8 | 193 | restart) |
6fd18a8c ER |
194 | stop |
195 | start | |
962e37bd | 196 | ;; |
4d4aed23 JB |
197 | reload|force-reload|try-restart) |
198 | if [ "$action" = "reload" ]; then | |
199 | # "reload" must not restart service - so let it reload only what's possible | |
200 | pgctlact="reload" | |
201 | else | |
202 | pgctlact="restart" | |
203 | fi | |
b5cf88e8 | 204 | for pgdir in $DB_CLUSTERS; do |
205 | pgstatus "$pgdir" | |
206 | if [ "$PG_STATUS" = "not running" ]; then | |
207 | msg_not_running "PostgreSQL $pgdir" | |
4d4aed23 JB |
208 | if [ "$action" != "try-restart" ]; then |
209 | RETVAL=7 | |
210 | fi | |
b5cf88e8 | 211 | else |
212 | msg_reloading "PostgreSQL $pgdir" | |
213 | busy | |
214 | # is postgresql really alive? | |
b3e56078 | 215 | if ps -p "$PG_PID" >/dev/null; then |
4d4aed23 | 216 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir $pgctlact 2>&1 >/dev/null" |
24852107 | 217 | pgstatus "$pgdir" |
b5cf88e8 | 218 | if [ "$PG_STATUS" = "running" ]; then |
9c6c5600 | 219 | ok |
24852107 | 220 | else |
9c6c5600 | 221 | fail |
24852107 | 222 | fi |
4d4aed23 JB |
223 | elif [ "$action" != "try-restart" ]; then |
224 | # postgresql died and pg_ctl has misinformed us about | |
225 | # the status - i.e. service is actually not running | |
226 | RETVAL=7 | |
4239f689 | 227 | else |
b5cf88e8 | 228 | # postgresql died and pg_ctl has misinformed us about |
229 | # the status; remove pid file and start it again | |
230 | deltext; died | |
231 | rm -f $pgdir/postmaster.pid | |
232 | pgstart "$pgdir" | |
4239f689 | 233 | fi |
b5cf88e8 | 234 | fi |
235 | done | |
236 | pgsubsys | |
4239f689 | 237 | ;; |
b5cf88e8 | 238 | init) |
239 | nls "Note: this is only simple init action for convenience." | |
240 | nls "If you want some non-standard options, consider using initdb(1)." | |
241 | echo | |
242 | for pgdir in $DB_CLUSTERS; do | |
243 | if [ -f $pgdir/PG_VERSION ]; then | |
244 | echo $(nls "Skipping existing cluster %s" "$pgdir") | |
245 | else | |
246 | echo $(nls "Initializing cluster %s" "$pgdir") | |
247 | mkdir -p $pgdir | |
248 | chmod 700 $pgdir | |
c06b99eb | 249 | chown postgres:postgres $pgdir |
d5258758 | 250 | LC_ALL=$PG_INIT_LOCALE TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir" |
b5cf88e8 | 251 | fi |
252 | done | |
253 | echo $(nls "REMEMBER to setup password for user \"postgres\"")'!' | |
254 | ;; | |
255 | *) | |
9c6c5600 | 256 | msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}" |
257 | exit 3 | |
d29ea5e3 | 258 | esac |
259 | ||
4d4aed23 | 260 | exit $RETVAL |