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