]>
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 | ||
c35cc882 | 21 | # Get service config |
dafcea45 | 22 | if [ -f /etc/sysconfig/postgresql ]; then |
24852107 | 23 | . /etc/sysconfig/postgresql |
24 | else | |
dd94d10a | 25 | nls "Error: %s not found" /etc/sysconfig/postgresql |
b5cf88e8 | 26 | nls " PostgreSQL can't be run." |
24852107 | 27 | exit 1 |
03af0381 | 28 | fi |
29 | ||
dafcea45 | 30 | if [ ! "$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 |
34 | fi | |
35 | ||
c35cc882 | 36 | # Check that networking is up. |
b5cf88e8 | 37 | if 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 | |
42 | else | |
43 | exit 0 | |
d584f4fd | 44 | fi |
d29ea5e3 | 45 | |
93ace391 | 46 | action="$1" |
47 | ||
48 | # any db cluster as command line argument? | |
49 | if [ $# -gt 1 ]; then | |
50 | shift | |
51 | # perform action for specified clusters only | |
52 | DB_CLUSTERS="$@" | |
53 | else | |
54 | DB_CLUSTERS="$PG_DB_CLUSTERS" | |
55 | fi | |
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 | 71 | pgstatus() { |
24852107 | 72 | PG_STATUS="unknown" |
73 | PG_PID="unknown" | |
14caa7e9 ER |
74 | output=$(LC_ALL=C TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $1 status") |
75 | status=$? | |
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? |
b3e56078 | 82 | PG_PID=$(echo "$output" | awk '/PID: / { match($0, "PID: [0-9]+"); print substr($0,RSTART+5,RLENGTH-5) }') |
24852107 | 83 | fi |
84 | } | |
85 | ||
ee1e3deb | 86 | # |
87 | # start postgresql and display appropriate messages | |
293d606e | 88 | # |
ee1e3deb | 89 | # arguments: |
90 | # $1 - db cluster | |
91 | # | |
92 | pgstart() { | |
93ace391 | 93 | msg_starting "PostgreSQL $1" |
cec778d4 | 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 | # | |
101 | pgsubsys() { | |
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 | |
6fd18a8c | 117 | start() { |
b5cf88e8 | 118 | for pgdir in $DB_CLUSTERS; do |
119 | pgstatus "$pgdir" | |
120 | if [ "$PG_STATUS" = "running" ]; then | |
121 | # pg_ctl status can misinform us about postgresql status | |
122 | # so let's check if postmaster is really alive | |
b3e56078 | 123 | if ps -p "$PG_PID" >/dev/null; then |
b5cf88e8 | 124 | msg_already_running "PostgreSQL $pgdir" |
24852107 | 125 | else |
b5cf88e8 | 126 | # pg_ctl has misinformed us about postgresql status; |
127 | # remove pid file and run postgresql | |
128 | msg_not_running "PostgreSQL $pgdir" | |
129 | rm -f $pgdir/postmaster.pid | |
93ace391 | 130 | pgstart "$pgdir" |
2a514bb5 | 131 | fi |
b5cf88e8 | 132 | else |
133 | pgstart "$pgdir" | |
134 | fi | |
135 | done | |
136 | pgsubsys | |
6fd18a8c ER |
137 | } |
138 | ||
139 | stop() { | |
b5cf88e8 | 140 | for pgdir in $DB_CLUSTERS; do |
141 | pgstatus "$pgdir" | |
142 | if [ "$PG_STATUS" = "not running" ]; then | |
143 | msg_not_running "PostgreSQL $pgdir" | |
144 | else | |
145 | msg_stopping "PostgreSQL $pgdir" | |
146 | busy | |
cec778d4 | 147 | # is postgresql really alive? |
b3e56078 | 148 | if ps -p "$PG_PID" >/dev/null; then |
b958caa9 | 149 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null" |
b5cf88e8 | 150 | pgstatus "$pgdir" |
151 | if [ "$PG_STATUS" != "not running" ]; then | |
152 | fail | |
24852107 | 153 | else |
b5cf88e8 | 154 | ok |
24852107 | 155 | fi |
b5cf88e8 | 156 | else |
157 | # postgresql is not really alive; pg_ctl misinformed us | |
158 | # about the status | |
159 | died | |
24852107 | 160 | fi |
b5cf88e8 | 161 | rm -f $pgdir/postmaster.pid |
162 | fi | |
163 | done | |
164 | pgsubsys | |
6fd18a8c ER |
165 | } |
166 | ||
167 | # See how we were called. | |
168 | # Every action is performed for all given (all configured by default) | |
169 | # db clusters. | |
170 | case "$action" in | |
171 | start) | |
172 | start | |
173 | ;; | |
174 | stop) | |
175 | stop | |
d29ea5e3 | 176 | ;; |
b5cf88e8 | 177 | status) |
178 | for pgdir in $DB_CLUSTERS; do | |
3fd1d6f0 | 179 | pgstatus "$pgdir" |
3fd1d6f0 | 180 | if [ "$PG_STATUS" = "running" ]; then |
1c07dbe4 | 181 | show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID" |
182 | progress "$PG_STATUS" | |
3fd1d6f0 | 183 | else |
1c07dbe4 | 184 | show "PostgreSQL cluster %s" "$pgdir" |
185 | progress "$PG_STATUS" "$CFAIL" | |
3fd1d6f0 | 186 | fi |
1c07dbe4 | 187 | echo |
3fd1d6f0 | 188 | done |
d29ea5e3 | 189 | ;; |
b5cf88e8 | 190 | restart) |
6fd18a8c ER |
191 | stop |
192 | start | |
962e37bd | 193 | ;; |
9c6c5600 | 194 | reload|force-reload) |
b5cf88e8 | 195 | for pgdir in $DB_CLUSTERS; do |
196 | pgstatus "$pgdir" | |
197 | if [ "$PG_STATUS" = "not running" ]; then | |
198 | msg_not_running "PostgreSQL $pgdir" | |
199 | else | |
200 | msg_reloading "PostgreSQL $pgdir" | |
201 | busy | |
202 | # is postgresql really alive? | |
b3e56078 | 203 | if ps -p "$PG_PID" >/dev/null; then |
b958caa9 | 204 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null" |
24852107 | 205 | pgstatus "$pgdir" |
b5cf88e8 | 206 | if [ "$PG_STATUS" = "running" ]; then |
9c6c5600 | 207 | ok |
24852107 | 208 | else |
9c6c5600 | 209 | fail |
24852107 | 210 | fi |
4239f689 | 211 | else |
b5cf88e8 | 212 | # postgresql died and pg_ctl has misinformed us about |
213 | # the status; remove pid file and start it again | |
214 | deltext; died | |
215 | rm -f $pgdir/postmaster.pid | |
216 | pgstart "$pgdir" | |
4239f689 | 217 | fi |
b5cf88e8 | 218 | fi |
219 | done | |
220 | pgsubsys | |
4239f689 | 221 | ;; |
b5cf88e8 | 222 | init) |
223 | nls "Note: this is only simple init action for convenience." | |
224 | nls "If you want some non-standard options, consider using initdb(1)." | |
225 | echo | |
226 | for pgdir in $DB_CLUSTERS; do | |
227 | if [ -f $pgdir/PG_VERSION ]; then | |
228 | echo $(nls "Skipping existing cluster %s" "$pgdir") | |
229 | else | |
230 | echo $(nls "Initializing cluster %s" "$pgdir") | |
231 | mkdir -p $pgdir | |
232 | chmod 700 $pgdir | |
c06b99eb | 233 | chown postgres:postgres $pgdir |
f1a02868 | 234 | TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir" |
b5cf88e8 | 235 | fi |
236 | done | |
237 | echo $(nls "REMEMBER to setup password for user \"postgres\"")'!' | |
238 | ;; | |
239 | *) | |
9c6c5600 | 240 | msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}" |
241 | exit 3 | |
d29ea5e3 | 242 | esac |
243 | ||
244 | exit 0 |