]>
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 | |
206d9ac7 | 79 | if [ $status -eq 3 ]; then |
24852107 | 80 | PG_STATUS="not running" |
6091e228 | 81 | elif [ $status -eq 0 ]; then |
24852107 | 82 | PG_STATUS="running" |
b8d42a8b | 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() { | |
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 | # | |
104 | pgsubsys() { | |
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 | 120 | start() { |
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 | ||
142 | stop() { | |
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 | ||
94b00d5a | 170 | |
4d4aed23 | 171 | RETVAL=0 |
6fd18a8c ER |
172 | # See how we were called. |
173 | # Every action is performed for all given (all configured by default) | |
174 | # db clusters. | |
175 | case "$action" in | |
176 | start) | |
b8d42a8b | 177 | start |
6fd18a8c ER |
178 | ;; |
179 | stop) | |
b8d42a8b | 180 | stop |
d29ea5e3 | 181 | ;; |
b5cf88e8 | 182 | status) |
183 | for pgdir in $DB_CLUSTERS; do | |
3fd1d6f0 | 184 | pgstatus "$pgdir" |
3fd1d6f0 | 185 | if [ "$PG_STATUS" = "running" ]; then |
1c07dbe4 | 186 | show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID" |
187 | progress "$PG_STATUS" | |
3fd1d6f0 | 188 | else |
1c07dbe4 | 189 | show "PostgreSQL cluster %s" "$pgdir" |
190 | progress "$PG_STATUS" "$CFAIL" | |
3fd1d6f0 | 191 | fi |
1c07dbe4 | 192 | echo |
3fd1d6f0 | 193 | done |
d29ea5e3 | 194 | ;; |
b5cf88e8 | 195 | restart) |
6fd18a8c ER |
196 | stop |
197 | start | |
962e37bd | 198 | ;; |
4d4aed23 JB |
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 | |
202 | pgctlact="reload" | |
203 | else | |
204 | pgctlact="restart" | |
205 | fi | |
b5cf88e8 | 206 | for pgdir in $DB_CLUSTERS; do |
207 | pgstatus "$pgdir" | |
208 | if [ "$PG_STATUS" = "not running" ]; then | |
209 | msg_not_running "PostgreSQL $pgdir" | |
4d4aed23 JB |
210 | if [ "$action" != "try-restart" ]; then |
211 | RETVAL=7 | |
212 | fi | |
b5cf88e8 | 213 | else |
214 | msg_reloading "PostgreSQL $pgdir" | |
215 | busy | |
216 | # is postgresql really alive? | |
b3e56078 | 217 | if ps -p "$PG_PID" >/dev/null; then |
4d4aed23 | 218 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir $pgctlact 2>&1 >/dev/null" |
24852107 | 219 | pgstatus "$pgdir" |
b5cf88e8 | 220 | if [ "$PG_STATUS" = "running" ]; then |
9c6c5600 | 221 | ok |
24852107 | 222 | else |
9c6c5600 | 223 | fail |
24852107 | 224 | fi |
4d4aed23 JB |
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 | |
228 | RETVAL=7 | |
4239f689 | 229 | else |
b5cf88e8 | 230 | # postgresql died and pg_ctl has misinformed us about |
231 | # the status; remove pid file and start it again | |
232 | deltext; died | |
233 | rm -f $pgdir/postmaster.pid | |
234 | pgstart "$pgdir" | |
4239f689 | 235 | fi |
b5cf88e8 | 236 | fi |
237 | done | |
238 | pgsubsys | |
4239f689 | 239 | ;; |
b5cf88e8 | 240 | init) |
241 | nls "Note: this is only simple init action for convenience." | |
242 | nls "If you want some non-standard options, consider using initdb(1)." | |
243 | echo | |
244 | for pgdir in $DB_CLUSTERS; do | |
245 | if [ -f $pgdir/PG_VERSION ]; then | |
246 | echo $(nls "Skipping existing cluster %s" "$pgdir") | |
247 | else | |
248 | echo $(nls "Initializing cluster %s" "$pgdir") | |
249 | mkdir -p $pgdir | |
250 | chmod 700 $pgdir | |
c06b99eb | 251 | chown postgres:postgres $pgdir |
b8d42a8b | 252 | LC_ALL=$PG_INIT_LOCALE TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE --data-checksums -D $pgdir" |
b5cf88e8 | 253 | fi |
254 | done | |
255 | echo $(nls "REMEMBER to setup password for user \"postgres\"")'!' | |
256 | ;; | |
257 | *) | |
9c6c5600 | 258 | msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}" |
259 | exit 3 | |
d29ea5e3 | 260 | esac |
261 | ||
4d4aed23 | 262 | exit $RETVAL |