]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
- fixed multi-line descriptions
[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 # Source function library
14 . /etc/rc.d/init.d/functions
15
16 # Get network config
17 . /etc/sysconfig/network
18
19 # Get service config
20 if [ -f /etc/sysconfig/postgresql ]; then
21         . /etc/sysconfig/postgresql
22 else
23         nls "Error: %s not found" /etc/sysconfig/postgresql
24         nls " PostgreSQL can't be run."
25         exit 1
26 fi
27
28 if [ ! "$PG_DB_CLUSTERS" ]; then
29         nls "Error: PG_DB_CLUSTERS not found or is empty"
30         nls " PostgreSQL can't be run."
31         exit 1
32 fi
33
34 # Check that networking is up.
35 if is_yes "${NETWORKING}"; then
36         if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
37                 msg_network_down PostgreSQL
38                 exit 1
39         fi
40 else
41         exit 0
42 fi
43
44 action="$1"
45
46 # any db cluster as command line argument?
47 if [ $# -gt 1 ]; then
48         shift
49         # perform action for specified clusters only
50         DB_CLUSTERS="$@"
51 else
52         DB_CLUSTERS="$PG_DB_CLUSTERS"
53 fi
54
55 #
56 # Useful functions.
57 #
58
59 #
60 # check for postgresql status
61 #
62 # arguments:
63 # $1 - db cluster
64 #
65 # sets variables:
66 # PG_STATUS = running | not running
67 # PG_PID    = pid of postmaster process
68 #
69 pgstatus() {
70         PG_STATUS="unknown"
71         PG_PID="unknown"
72         status=`/usr/bin/pg_ctl -D $1 status`
73
74         if echo "$status" | grep -q 'pg_ctl: postmaster or postgres is not running'; then
75                 PG_STATUS="not running"
76         elif echo "$status" | grep -q 'pg_ctl: postmaster is running'; then
77                 PG_STATUS="running"
78                 PG_PID=`echo "$status" | sed 's/^pg_ctl:.*pid: \([0-9]\+\).*/\1/' | head -1`
79         fi
80 }
81
82 #
83 # start postgresql and display appropriate messages
84 #
85 # arguments:
86 # $1 - db cluster
87 #
88 pgstart() {
89         msg_starting "PostgreSQL $1"
90         busy
91         TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null"
92         sleep 1
93         pgstatus "$1"
94         if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then
95                 ok
96         else
97                 fail 
98         fi
99 }
100
101 #
102 # check for running postgresql instances; if any instance is running then
103 # create subsys lock file
104 #
105 pgsubsys() {
106         # check for every defined db cluster in sysconfig file
107         for pgdir in $PG_DB_CLUSTERS; do
108                 pgstatus "$pgdir"
109                 if [ "$PG_STATUS" = "running" ]; then
110                         touch /var/lock/subsys/postgresql
111                         return
112                 fi
113         done
114         rm -f /var/lock/subsys/postgresql
115 }
116
117 #
118 # End of useful functions.
119 #
120
121
122 # See how we were called.
123 # Every action is performed for all given (all configured by default)
124 # db clusters.
125 case "$action" in
126   start)
127         for pgdir in $DB_CLUSTERS; do
128                 pgstatus "$pgdir"
129                 if [ "$PG_STATUS" = "running" ]; then
130                         # pg_ctl status can misinform us about postgresql status
131                         # so let's check if postmaster is really alive
132                         if ps ax | grep -v grep | grep -q "$PG_PID"; then
133                                 msg_already_running "PostgreSQL $pgdir"
134                         else
135                                 # pg_ctl has misinformed us about postgresql status;
136                                 # remove pid file and run postgresql
137                                 msg_not_running "PostgreSQL $pgdir"
138                                 rm -f $pgdir/postmaster.pid
139                                 pgstart "$pgdir"
140                         fi
141                 else
142                         pgstart "$pgdir"
143                 fi
144         done
145         pgsubsys
146         ;;
147   stop)
148         for pgdir in $DB_CLUSTERS; do
149                 pgstatus "$pgdir"
150                 if [ "$PG_STATUS" = "not running" ]; then
151                         msg_not_running "PostgreSQL $pgdir"
152                 else
153                         msg_stopping "PostgreSQL $pgdir"
154                         busy
155                                 # is postgresql really alive?
156                         if ps ax | grep -v grep | grep -q "$PG_PID"; then
157                                 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
158                                 pgstatus "$pgdir"
159                                 if [ "$PG_STATUS" != "not running" ]; then
160                                         fail
161                                 else
162                                         ok
163                                 fi
164                         else
165                                 # postgresql is not really alive; pg_ctl misinformed us
166                                 # about the status
167                                 died
168                         fi
169                         rm -f $pgdir/postmaster.pid
170                 fi
171         done
172         pgsubsys
173         ;;
174   status)
175         for pgdir in $DB_CLUSTERS; do
176         TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
177                 done
178         ;;
179   restart)
180         $0 stop "$DB_CLUSTERS"
181         $0 start "$DB_CLUSTERS"
182         ;;
183   reload|force-reload)
184         for pgdir in $DB_CLUSTERS; do
185                 pgstatus "$pgdir"
186                 if [ "$PG_STATUS" = "not running" ]; then
187                         msg_not_running "PostgreSQL $pgdir"
188                 else
189                         msg_reloading "PostgreSQL $pgdir"
190                         busy
191                         # is postgresql really alive?
192                         if ps ax | grep -v grep | grep -q "$PG_PID"; then
193                                 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null"
194                                         pgstatus "$pgdir"
195                                 if [ "$PG_STATUS" = "running" ]; then
196                                         ok
197                                 else
198                                         fail
199                                 fi
200                         else
201                                 # postgresql died and pg_ctl has misinformed us about
202                                 # the status; remove pid file and start it again
203                                 deltext; died
204                                 rm -f $pgdir/postmaster.pid
205                                 pgstart "$pgdir"
206                         fi
207                 fi
208         done
209         pgsubsys
210         ;;
211   init)
212         nls "Note: this is only simple init action for convenience."
213         nls "If you want some non-standard options, consider using initdb(1)."
214         echo
215         for pgdir in $DB_CLUSTERS; do
216                 if [ -f $pgdir/PG_VERSION ]; then
217                         echo $(nls "Skipping existing cluster %s" "$pgdir")
218                 else
219                         echo $(nls "Initializing cluster %s" "$pgdir")
220                         mkdir -p $pgdir
221                         chmod 700 $pgdir
222                         chown postgres.postgres $pgdir
223                         TMPDIR=/tmp su - postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir"
224                 fi
225         done
226         echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
227         ;;
228   *)
229         msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"
230         exit 3
231 esac
232
233 exit 0
This page took 0.067577 seconds and 4 git commands to generate.