]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
da736cc0e50ea61e0c6afbf6ada7d5eeec81a40e
[packages/postgresql.git] / postgresql.init
1 #!/bin/sh
2 #
3 # postgresql    This is the init script for starting up the PostgreSQL
4 #               server
5 #
6 # chkconfig:    345 84 25
7 #
8 # description:  Starts and stops the PostgreSQL backend daemon that handles \
9 #               all database requests.
10 #
11 # processname:  postmaster
12
13 cd /
14
15 # Source function library
16 . /etc/rc.d/init.d/functions
17
18 # Get network config
19 . /etc/sysconfig/network
20
21 PG_INIT_LOCALE=C
22
23 # Get service config
24 if [ -f /etc/sysconfig/postgresql ]; then
25         . /etc/sysconfig/postgresql
26 else
27         nls "Error: %s not found" /etc/sysconfig/postgresql
28         nls " PostgreSQL can't be run."
29         exit 1
30 fi
31
32 if [ ! "$PG_DB_CLUSTERS" ]; then
33         nls "Error: PG_DB_CLUSTERS not found or is empty"
34         nls " PostgreSQL can't be run."
35         exit 1
36 fi
37
38 # Check that networking is up.
39 if is_yes "${NETWORKING}"; then
40         if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
41                 msg_network_down PostgreSQL
42                 exit 1
43         fi
44 else
45         exit 0
46 fi
47
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
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 #
73 pgstatus() {
74         PG_STATUS="unknown"
75         PG_PID="unknown"
76         output=$(LC_ALL=C TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $1 status")
77         status=$?
78
79         if [ $status -eq 1 ]; then
80                 PG_STATUS="not running"
81         elif [ $status -eq 0 ]; then
82                 PG_STATUS="running"
83         # or maybe grab it from postmaster.pid file?
84                 PG_PID=$(echo "$output" | awk '/PID: / { match($0, "PID: [0-9]+"); print substr($0,RSTART+5,RLENGTH-5) }')
85         fi
86 }
87
88 #
89 # start postgresql and display appropriate messages
90 #
91 # arguments:
92 # $1 - db cluster
93 #
94 pgstart() {
95         is_no "$RC_LOGGING" && fork=--fork
96         msg_starting "PostgreSQL $1"
97         daemon $fork --user postgres /usr/bin/pg_ctl -s -w -D $1 start
98 }
99
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 }
115
116 #
117 # End of useful functions.
118 #
119
120 start() {
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
126                         if ps -p "$PG_PID" >/dev/null; then
127                                 msg_already_running "PostgreSQL $pgdir"
128                         else
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
133                                 pgstart "$pgdir"
134                         fi
135                 else
136                         pgstart "$pgdir"
137                 fi
138         done
139         pgsubsys
140 }
141
142 stop() {
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
150                         # is postgresql really alive?
151                         if ps -p "$PG_PID" >/dev/null; then
152                                 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
153                                 pgstatus "$pgdir"
154                                 if [ "$PG_STATUS" != "not running" ]; then
155                                         fail
156                                 else
157                                         ok
158                                 fi
159                         else
160                                 # postgresql is not really alive; pg_ctl misinformed us
161                                 # about the status
162                                 died
163                         fi
164                         rm -f $pgdir/postmaster.pid
165                 fi
166         done
167         pgsubsys
168 }
169
170 RETVAL=0
171 # See how we were called.
172 # Every action is performed for all given (all configured by default)
173 # db clusters.
174 case "$action" in
175   start)
176         start
177         ;;
178   stop)
179         stop
180         ;;
181   status)
182         for pgdir in $DB_CLUSTERS; do
183                 pgstatus "$pgdir"
184                 if [ "$PG_STATUS" = "running" ]; then
185                         show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID"
186                         progress "$PG_STATUS"
187                 else
188                         show "PostgreSQL cluster %s" "$pgdir"
189                         progress "$PG_STATUS" "$CFAIL"
190                 fi
191                 echo
192         done
193         ;;
194   restart)
195         stop
196         start
197         ;;
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
205         for pgdir in $DB_CLUSTERS; do
206                 pgstatus "$pgdir"
207                 if [ "$PG_STATUS" = "not running" ]; then
208                         msg_not_running "PostgreSQL $pgdir"
209                         if [ "$action" != "try-restart" ]; then
210                                 RETVAL=7
211                         fi
212                 else
213                         msg_reloading "PostgreSQL $pgdir"
214                         busy
215                         # is postgresql really alive?
216                         if ps -p "$PG_PID" >/dev/null; then
217                                 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir $pgctlact 2>&1 >/dev/null"
218                                         pgstatus "$pgdir"
219                                 if [ "$PG_STATUS" = "running" ]; then
220                                         ok
221                                 else
222                                         fail
223                                 fi
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
228                         else
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"
234                         fi
235                 fi
236         done
237         pgsubsys
238         ;;
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
250                         chown postgres:postgres $pgdir
251                         LC_ALL=$PG_INIT_LOCALE TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir"
252                 fi
253         done
254         echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
255         ;;
256   *)
257         msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"
258         exit 3
259 esac
260
261 exit $RETVAL
This page took 0.346297 seconds and 2 git commands to generate.