]> TLD Linux GIT Repositories - packages/postgresql.git/blob - postgresql.init
- updated to 13.8
[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-@pgsqlversion@ ]; then
25         . /etc/sysconfig/postgresql-@pgsqlversion@
26 else
27         nls "Error: %s not found" /etc/sysconfig/postgresql-@pgsqlversion@
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 "@pgsqlbindir@/pg_ctl -D $1 status")
77         status=$?
78
79         if [ $status -eq 3 ]; 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 @pgsqlbindir@/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-@pgsqlversion@
110                         return
111                 fi
112         done
113         rm -f /var/lock/subsys/postgresql-@pgsqlversion@
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" 1>/dev/null 2>&1; 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" 1>/dev/null 2>&1; then
152                                 TMPDIR=/tmp su postgres -c "@pgsqlbindir@/pg_ctl -w -D $pgdir stop -m fast 1>/dev/null 2>&1"
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
171 RETVAL=0
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)
177         start
178         ;;
179   stop)
180         stop
181         ;;
182   status)
183         for pgdir in $DB_CLUSTERS; do
184                 pgstatus "$pgdir"
185                 if [ "$PG_STATUS" = "running" ]; then
186                         show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID"
187                         progress "$PG_STATUS"
188                 else
189                         show "PostgreSQL cluster %s" "$pgdir"
190                         progress "$PG_STATUS" "$CFAIL"
191                 fi
192                 echo
193         done
194         ;;
195   restart)
196         stop
197         start
198         ;;
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
206         for pgdir in $DB_CLUSTERS; do
207                 pgstatus "$pgdir"
208                 if [ "$PG_STATUS" = "not running" ]; then
209                         msg_not_running "PostgreSQL $pgdir"
210                         if [ "$action" != "try-restart" ]; then
211                                 RETVAL=7
212                         fi
213                 else
214                         msg_reloading "PostgreSQL $pgdir"
215                         busy
216                         # is postgresql really alive?
217                         if ps -p "$PG_PID" 1>/dev/null 2>&1; then
218                                 TMPDIR=/tmp su postgres -c "@pgsqlbindir@/pg_ctl -D $pgdir $pgctlact 1>/dev/null 2>&1"
219                                         pgstatus "$pgdir"
220                                 if [ "$PG_STATUS" = "running" ]; then
221                                         ok
222                                 else
223                                         fail
224                                 fi
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
229                         else
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"
235                         fi
236                 fi
237         done
238         pgsubsys
239         ;;
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
251                         chown postgres:postgres $pgdir
252                         LC_ALL=$PG_INIT_LOCALE TMPDIR=/tmp su postgres -s /bin/sh -c "@pgsqlbindir@/initdb -E UNICODE --data-checksums -D $pgdir"
253                 fi
254         done
255         echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
256         ;;
257   *)
258         msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"
259         exit 3
260 esac
261
262 exit $RETVAL