]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
- use C locale for initalization by default; it's safer than other loclaes; https...
[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         msg_starting "PostgreSQL $1"
96         daemon --user postgres /usr/bin/pg_ctl -s -w -D $1 start
97 }
98
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 }
114
115 #
116 # End of useful functions.
117 #
118
119 start() {
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
125                         if ps -p "$PG_PID" >/dev/null; then
126                                 msg_already_running "PostgreSQL $pgdir"
127                         else
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
132                                 pgstart "$pgdir"
133                         fi
134                 else
135                         pgstart "$pgdir"
136                 fi
137         done
138         pgsubsys
139 }
140
141 stop() {
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
149                         # is postgresql really alive?
150                         if ps -p "$PG_PID" >/dev/null; then
151                                 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
152                                 pgstatus "$pgdir"
153                                 if [ "$PG_STATUS" != "not running" ]; then
154                                         fail
155                                 else
156                                         ok
157                                 fi
158                         else
159                                 # postgresql is not really alive; pg_ctl misinformed us
160                                 # about the status
161                                 died
162                         fi
163                         rm -f $pgdir/postmaster.pid
164                 fi
165         done
166         pgsubsys
167 }
168
169 RETVAL=0
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
179         ;;
180   status)
181         for pgdir in $DB_CLUSTERS; do
182                 pgstatus "$pgdir"
183                 if [ "$PG_STATUS" = "running" ]; then
184                         show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID"
185                         progress "$PG_STATUS"
186                 else
187                         show "PostgreSQL cluster %s" "$pgdir"
188                         progress "$PG_STATUS" "$CFAIL"
189                 fi
190                 echo
191         done
192         ;;
193   restart)
194         stop
195         start
196         ;;
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
204         for pgdir in $DB_CLUSTERS; do
205                 pgstatus "$pgdir"
206                 if [ "$PG_STATUS" = "not running" ]; then
207                         msg_not_running "PostgreSQL $pgdir"
208                         if [ "$action" != "try-restart" ]; then
209                                 RETVAL=7
210                         fi
211                 else
212                         msg_reloading "PostgreSQL $pgdir"
213                         busy
214                         # is postgresql really alive?
215                         if ps -p "$PG_PID" >/dev/null; then
216                                 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir $pgctlact 2>&1 >/dev/null"
217                                         pgstatus "$pgdir"
218                                 if [ "$PG_STATUS" = "running" ]; then
219                                         ok
220                                 else
221                                         fail
222                                 fi
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
227                         else
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"
233                         fi
234                 fi
235         done
236         pgsubsys
237         ;;
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
249                         chown postgres:postgres $pgdir
250                         LC_ALL=$PG_INIT_LOCALE TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir"
251                 fi
252         done
253         echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
254         ;;
255   *)
256         msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"
257         exit 3
258 esac
259
260 exit $RETVAL
This page took 0.101323 seconds and 4 git commands to generate.