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