]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
- added support for ssl
[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 85 15
7 #
8 # description:  Starts and stops the PostgreSQL backend daemon that handles \
9 #               all database requests.
10 #
11 # processname:  postmaster
12 # pidfile:      /var/run/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 # Get service config
21 if [ -f /etc/sysconfig/postgresql ] ; then
22         . /etc/sysconfig/postgresql
23 else
24         ALLOW_TCP_CONNECTIONS=no
25         ALLOW_USE_SSL=no
26         POSTGRES_DATA_DIR=/var/lib/pgsql
27         POSTGRES_LOG=/var/log/pgsql
28 fi
29
30 # Check that networking is up.
31 if is_no "${NETWORKING}"; then
32         msg_Network_Down Postgresql
33         exit 1
34 fi
35
36 # check if postmaster is realy alive
37 if [ -f /var/lib/pgsql/postmaster.pid ]; then
38  if ps -p `head -1 /var/lib/pgsql/postmaster.pid` > /dev/null ; then
39          #ok. it's alive
40         echo > /dev/null;
41  else
42          # oh, it has crashed
43          rm -f  /var/lock/subsys/postgresql
44  fi
45 else
46         # it's stoped. 
47         rm -f  /var/lock/subsys/postgresql
48 fi
49
50
51 # See how we were called.
52 case "$1" in
53   start)
54         # Check if the service is already running?
55         if [ -f /var/lock/subsys/postgresql ]; then
56                 msg_Already_Running PostgreSQL
57         else    
58                 if is_yes "$ALLOW_USE_SSL"; then
59                         if is_no "$ALLOW_TCP_CONNECTIONS"; then
60                                 nls "You must enable ALLOW_TCP_CONNECTIONS in order to use SSL"
61                                 exit 1
62                         elif [ ! -f "$POSTGRES_DATA_DIR/server.key" ]; then
63                                 nls "$POSTGRES_DATA_DIR/server.key not found!"
64                                 exit 1
65                         elif [ ! -f "$POSTGRES_DATA_DIR/server.crt" ]; then
66                                 nls "$POSTGRES_DATA_DIR/server.crt not found!"
67                                 exit 1
68                         fi
69                 fi
70                 
71                 # Sanity check
72                 [ -f /tmp/.s.PGSQL.5432 ] || rm -f /tmp/.s.PGSQL.5432
73                 
74                 # Check for the PGDATA structure
75                 if [ -f $POSTGRES_DATA_DIR/PG_VERSION ] && [ -f $POSTGRES_DATA_DIR/global/pg_control ]; then
76                     # Check version of existing PGDATA
77                     if [ `cat $POSTGRES_DATA_DIR/PG_VERSION` != '7.1' ]; then
78                         show "Databases created under incompatibile version. Migrate them first."; fail
79                         exit 1
80                     fi
81                 else
82                     # Create sample database
83                     if [ ! -d $POSTGRES_DATA_DIR ]; then
84                         mkdir -p $POSTGRES_DATA_DIR
85                         chown postgres.postgres $POSTGRES_DATA_DIR
86                         chmod 700 $POSTGRES_DATA_DIR
87                     fi
88                     TMPDIR=/tmp su postgres -c "LD_LIBRARY_PATH=/usr/lib \
89                         initdb --pgdata=$POSTGRES_DATA_DIR \
90                         -L /usr/share/postgresql"
91                 fi
92                 
93                 msg_starting PostgreSQL 
94                 busy
95                 PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
96                     $(is_yes "$POSTGRES_QUIET" && echo '-s')";
97                 POSTMASTER_OPTIONS=" \
98                     $(is_yes "$POSTGRES_QUIET" && echo '-S') \
99                     $(is_yes "$POSTGRES_DISABLE_FSYNC" && echo '-F') \
100                     $(is_yes "$ALLOW_TCP_CONNECTIONS" && echo '-i') \
101                     $(is_yes "$ALLOW_USE_SSL" && echo '-l')";
102
103                 TMPDIR=/tmp su postgres -c "\
104                         $(echo "/usr/bin/pg_ctl start $PGSQL_CMDLINE -l $POSTGRES_LOG") \
105                         $([ "$POSTMASTER_OPTIONS". != . ] && echo "-o '$POSTMASTER_OPTIONS'") \
106                         $(echo "2>&1 >/dev/null")";
107
108                 out=`status postmaster`
109                 if [ $? -eq 0 ]; then 
110                     deltext; ok
111                 else
112                     deltext; fail 
113                     exit 1      
114                 fi
115         fi
116         touch /var/lock/subsys/postgresql
117         ;;
118   stop)
119
120         if [ ! -f /var/lock/subsys/postgresql ]; then
121                 msg_Not_Running PostgreSQL
122                 exit 0  
123         fi
124         msg_stopping postgreSQL
125         busy
126         PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")";
127         TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl stop $PGSQL_CMDLINE -m fast 2>&1 >/dev/null"
128         out=`status postmaster`
129         if [ $? -eq 0 ]; then
130                 deltext; fail
131                 echo -e "\n$out";
132                 exit 1
133         else
134                 deltext; ok
135         fi
136         rm -f /var/run/postmaster.pid
137         rm -f /var/lock/subsys/postgresql
138         ;;
139   status)
140         PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")";
141         TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl status $PGSQL_CMDLINE"
142         ;;
143   restart)
144         $0 stop
145         $0 start
146         ;;
147   reload)
148         if [ ! -f /var/lock/subsys/postgresql ]; then
149                 msg_Not_Running PostgreSQL
150                 exit 0  
151         fi
152         msg_reloading postgreSQL
153         busy
154
155         PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
156             $([ "$POSTGRES_QUIET". = yes. ] && echo '-s')";
157         POSTMASTER_OPTIONS=" \
158             $([ "$POSTGRES_QUIET". = yes. ] && echo '-S') \
159             $([ "$POSTGRES_DISABLE_FSYNC". = yes. ] && echo '-F') \
160             $([ "$ALLOW_TCP_CONNECTIONS". = yes. ] && echo '-i')";
161
162         TMPDIR=/tmp su postgres -c "\
163                 $(echo "/usr/bin/pg_ctl restart $PGSQL_CMDLINE -l $POSTGRES_LOG") \
164                 $([ "$POSTMASTER_OPTIONS". != . ] && echo "-o '$POSTMASTER_OPTIONS'") \
165                 $(echo "2>&1 >/dev/null")";
166
167         out=`status postmaster`
168         if [ $? -eq 0 ]; then
169                 deltext; ok
170         else
171                 deltext; fail
172                 echo -e "\n$out";
173                 exit 1
174         fi
175         ;;
176   *)
177         msg_Usage "$0 {start|stop|status|restart|reload}"
178         exit 1
179 esac
180
181 exit 0
This page took 0.056888 seconds and 4 git commands to generate.