3 # postgresql This is the init script for starting up the PostgreSQL
8 # description: Starts and stops the PostgreSQL backend daemon that handles \
9 # all database requests.
11 # processname: postmaster
12 # pidfile: $POSTGRES_DATA_DIR/postmaster.pid
14 # Source function library
15 . /etc/rc.d/init.d/functions
18 . /etc/sysconfig/network
20 # Service config defaults
21 ALLOW_TCP_CONNECTIONS=no
23 POSTGRES_DATA_DIR=/var/lib/pgsql
24 POSTGRES_LOG=/var/log/pgsql
28 if [ -f /etc/sysconfig/postgresql ] ; then
29 . /etc/sysconfig/postgresql
32 # Check that networking is up.
33 if is_no "${NETWORKING}"; then
34 msg_network_down PostgreSQL
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
45 rm -f /var/lock/subsys/postgresql
49 rm -f /var/lock/subsys/postgresql
53 # See how we were called.
56 # Check if the service is already running?
57 if [ -f /var/lock/subsys/postgresql ]; then
58 msg_already_running PostgreSQL
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"
64 elif [ ! -f "$POSTGRES_DATA_DIR/server.key" ]; then
65 nls "$POSTGRES_DATA_DIR/server.key not found!"
67 elif [ ! -f "$POSTGRES_DATA_DIR/server.crt" ]; then
68 nls "$POSTGRES_DATA_DIR/server.crt not found!"
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
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
85 echo "Postgresql database not initialized. Try \"$0 init\" before start."
89 msg_starting PostgreSQL
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)";
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")";
106 out=`status postmaster`
107 if [ $? -eq 0 ]; then
114 touch /var/lock/subsys/postgresql
118 if [ ! -f /var/lock/subsys/postgresql ]; then
119 msg_not_running PostgreSQL
122 msg_stopping PostgreSQL
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
134 rm -f $POSTGRES_DATA_DIR/postmaster.pid
135 rm -f /var/lock/subsys/postgresql
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"
146 if [ ! -f /var/lock/subsys/postgresql ]; then
147 msg_not_running PostgreSQL
150 msg_reloading PostgreSQL
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')";
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")";
165 out=`status postmaster`
166 if [ $? -eq 0 ]; then
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."
181 echo -n "Specify encoding for pgsql database (eg: LATIN2, KOI8) [$ENCODING]: "
183 ENCODING=${new_encoding:-$ENCODING}
185 show "Creating initial postgresql database using $ENCODING encoding"
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
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"
201 msg_usage "$0 {start|stop|status|restart|reload}"