]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
- typos fixed, LATIN2 as example
[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 # pidfile:      $POSTGRES_DATA_DIR/postmaster.pid
13
14 # Source function library
15 . /etc/rc.d/init.d/functions
16
17 # Get network config
18 . /etc/sysconfig/network
19
20 # Service config defaults
21 ALLOW_TCP_CONNECTIONS=no
22 ALLOW_USE_SSL=no
23 POSTGRES_DATA_DIR=/var/lib/pgsql
24 POSTGRES_LOG=/var/log/pgsql
25 ENCODING=UNICODE
26
27 # Get service config
28 if [ -f /etc/sysconfig/postgresql ] ; then
29         . /etc/sysconfig/postgresql
30 fi
31
32 # Check that networking is up.
33 if is_no "${NETWORKING}"; then
34         msg_network_down PostgreSQL
35         exit 1
36 fi
37
38 # check if postmaster is realy alive
39 if [ -f $POSTGRES_DATA_DIR/postmaster.pid ]; then
40     if ps -p `head -1 $POSTGRES_DATA_DIR/postmaster.pid` > /dev/null ; then
41         # ok. it's alive
42         echo > /dev/null;
43     else
44         # oh, it has crashed
45         rm -f  /var/lock/subsys/postgresql
46     fi
47 else
48         # it's stopped.
49         rm -f  /var/lock/subsys/postgresql
50 fi
51
52
53 # See how we were called.
54 case "$1" in
55   start)
56         # Check if the service is already running?
57         if [ -f /var/lock/subsys/postgresql ]; then
58                 msg_already_running PostgreSQL
59         else    
60                 if is_yes "$ALLOW_USE_SSL"; then
61                         if is_no "$ALLOW_TCP_CONNECTIONS"; then
62                                 nls "You must enable ALLOW_TCP_CONNECTIONS in order to use SSL"
63                                 exit 1
64                         elif [ ! -f "$POSTGRES_DATA_DIR/server.key" ]; then
65                                 nls "$POSTGRES_DATA_DIR/server.key not found!"
66                                 exit 1
67                         elif [ ! -f "$POSTGRES_DATA_DIR/server.crt" ]; then
68                                 nls "$POSTGRES_DATA_DIR/server.crt not found!"
69                                 exit 1
70                         fi
71                 fi
72                 
73                 # Sanity check
74                 [ -e /tmp/.s.PGSQL.5432 ] || rm -f /tmp/.s.PGSQL.5432
75                 [ -f /tmp/.s.PGSQL.5432.lock ] || rm -f /tmp/.s.PGSQL.5432.lock
76                 
77                 # Check for the PGDATA structure
78                 if [ -f $POSTGRES_DATA_DIR/PG_VERSION ] && [ -f $POSTGRES_DATA_DIR/global/pg_control ]; then
79                     # Check version of existing PGDATA
80                     if [ `cat $POSTGRES_DATA_DIR/PG_VERSION` != '7.2' ]; then
81                         show "Databases created under incompatibile version. Migrate them first."; fail
82                         exit 1
83                     fi
84                 else
85                     echo "Postgresql database not initialized. Try \"$0 init\" before start."
86                     exit 1
87                 fi
88                 
89                 msg_starting PostgreSQL 
90                 busy
91                 PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
92                     $(is_yes "$POSTGRES_QUIET" && echo '-s')";
93                 POSTMASTER_OPTIONS=" \
94                     $(is_yes "$POSTGRES_QUIET" && echo '-S') \
95                     $(is_yes "$POSTGRES_DISABLE_FSYNC" && echo '-F') \
96                     $(is_yes "$ALLOW_TCP_CONNECTIONS" && echo '-i') \
97                     $(is_yes "$ALLOW_USE_SSL" && echo '-l') \
98                     $([ "$BUFFERS" ] && echo \-B $BUFFERS) \
99                     $([ "$MAXCONN" ] && echo \-N $MAXCONN)";
100
101                 TMPDIR=/tmp su postgres -c "\
102                         $(echo "/usr/bin/pg_ctl start -w $PGSQL_CMDLINE -l $POSTGRES_LOG") \
103                         $([ "$POSTMASTER_OPTIONS". != . ] && echo "-o '$POSTMASTER_OPTIONS'") \
104                         $(echo "2>&1 >/dev/null")";
105                 
106                 out=`status postmaster`
107                 if [ $? -eq 0 ]; then 
108                     deltext; ok
109                 else
110                     deltext; fail 
111                     exit 1      
112                 fi
113         fi
114         touch /var/lock/subsys/postgresql
115         ;;
116   stop)
117
118         if [ ! -f /var/lock/subsys/postgresql ]; then
119                 msg_not_running PostgreSQL
120                 exit 0  
121         fi
122         msg_stopping PostgreSQL
123         busy
124         PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")";
125         TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl stop $PGSQL_CMDLINE -m fast 2>&1 >/dev/null"
126         out=`status postmaster`
127         if [ $? -eq 0 ]; then
128                 deltext; fail
129                 echo -e "\n$out";
130                 exit 1
131         else
132                 deltext; ok
133         fi
134         rm -f $POSTGRES_DATA_DIR/postmaster.pid
135         rm -f /var/lock/subsys/postgresql
136         ;;
137   status)
138         PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")";
139         TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl status $PGSQL_CMDLINE"
140         ;;
141   restart)
142         $0 stop
143         $0 start
144         ;;
145   reload)
146         if [ ! -f /var/lock/subsys/postgresql ]; then
147                 msg_not_running PostgreSQL
148                 exit 0  
149         fi
150         msg_reloading PostgreSQL
151         busy
152
153         PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
154             $([ "$POSTGRES_QUIET". = yes. ] && echo '-s')";
155         POSTMASTER_OPTIONS=" \
156             $([ "$POSTGRES_QUIET". = yes. ] && echo '-S') \
157             $([ "$POSTGRES_DISABLE_FSYNC". = yes. ] && echo '-F') \
158             $([ "$ALLOW_TCP_CONNECTIONS". = yes. ] && echo '-i')";
159
160         TMPDIR=/tmp su postgres -c "\
161                 $(echo "/usr/bin/pg_ctl restart $PGSQL_CMDLINE -l $POSTGRES_LOG") \
162                 $([ "$POSTMASTER_OPTIONS". != . ] && echo "-o '$POSTMASTER_OPTIONS'") \
163                 $(echo "2>&1 >/dev/null")";
164
165         out=`status postmaster`
166         if [ $? -eq 0 ]; then
167                 deltext; ok
168         else
169                 deltext; fail
170                 echo -e "\n$out";
171                 exit 1
172         fi
173         ;;
174     init)
175         if [ -f "$POSTGRES_DATA_DIR/PG_VERSION" ]; then
176             echo "Seems that database is initialized now. Remove by hand $POSTGRES_DATA_DIR"
177             echo "before initialize database."
178             exit 1
179         fi
180
181         echo -n "Specify encoding for pgsql database (eg: LATIN2, KOI8) [$ENCODING]: "
182         read new_encoding
183         ENCODING=${new_encoding:-$ENCODING}
184
185         show "Creating initial postgresql database using $ENCODING encoding"
186         busy
187
188         # Create sample database
189         if [ ! -d $POSTGRES_DATA_DIR ]; then
190             mkdir -p $POSTGRES_DATA_DIR
191             chown postgres.postgres $POSTGRES_DATA_DIR
192             chmod 700 $POSTGRES_DATA_DIR
193         fi
194
195         
196         TMPDIR=/tmp su - postgres -s /bin/sh -c "LD_LIBRARY_PATH=/usr/lib \
197                         initdb --pgdata=$POSTGRES_DATA_DIR \
198                         -L /usr/share/postgresql -E $ENCODING"
199         ;;
200   *)
201         msg_usage "$0 {start|stop|status|restart|reload}"
202         exit 1
203 esac
204
205 exit 0
This page took 0.094081 seconds and 4 git commands to generate.