]> git.pld-linux.org Git - packages/postgresql.git/blobdiff - postgresql.init
- more things, reminder about password etc
[packages/postgresql.git] / postgresql.init
index 28b2c798c049e32400e17b9589f3f096b7e9b2e4..11343041dca1609288816b5b0fee56e645135f87 100644 (file)
@@ -3,13 +3,13 @@
 # postgresql   This is the init script for starting up the PostgreSQL
 #              server
 #
-# chkconfig:   345 85 15
+# chkconfig:   345 84 25
 #
 # description: Starts and stops the PostgreSQL backend daemon that handles \
 #              all database requests.
 #
 # processname: postmaster
-# pidfile:     /var/run/postmaster.pid
+# pidfile:     $POSTGRES_DATA_DIR/postmaster.pid
 
 # Source function library
 . /etc/rc.d/init.d/functions
 # Get network config
 . /etc/sysconfig/network
 
+# Service config defaults
+ALLOW_TCP_CONNECTIONS=no
+ALLOW_USE_SSL=no
+POSTGRES_DATA_DIR=/var/lib/pgsql
+POSTGRES_LOG=/var/log/pgsql
+ENCODING=UNICODE
+
 # Get service config
 if [ -f /etc/sysconfig/postgresql ] ; then
         . /etc/sysconfig/postgresql
-else
-        ALLOW_TCP_CONNECTIONS=no
-       POSTGRES_DATA_DIR=/var/state/psql
 fi
 
 # Check that networking is up.
 if is_no "${NETWORKING}"; then
-       msg_Network_Down Postgresql
+       msg_network_down PostgreSQL
        exit 1
 fi
 
+# check if postmaster is realy alive
+if [ -f $POSTGRES_DATA_DIR/postmaster.pid ]; then
+    if ps -p `head -1 $POSTGRES_DATA_DIR/postmaster.pid` > /dev/null ; then
+       # ok. it's alive
+       echo > /dev/null;
+    else
+       # oh, it has crashed
+       rm -f  /var/lock/subsys/postgresql
+    fi
+else
+       # it's stopped.
+       rm -f  /var/lock/subsys/postgresql
+fi
+
+
 # See how we were called.
 case "$1" in
   start)
        # Check if the service is already running?
-       if [ ! -f /var/lock/subsys/postgresql ]; then
-
+       if [ -f /var/lock/subsys/postgresql ]; then
+               msg_already_running PostgreSQL
+       else    
+               if is_yes "$ALLOW_USE_SSL"; then
+                       if is_no "$ALLOW_TCP_CONNECTIONS"; then
+                               nls "You must enable ALLOW_TCP_CONNECTIONS in order to use SSL"
+                               exit 1
+                       elif [ ! -f "$POSTGRES_DATA_DIR/server.key" ]; then
+                               nls "$POSTGRES_DATA_DIR/server.key not found!"
+                               exit 1
+                       elif [ ! -f "$POSTGRES_DATA_DIR/server.crt" ]; then
+                               nls "$POSTGRES_DATA_DIR/server.crt not found!"
+                               exit 1
+                       fi
+               fi
+               
                # Sanity check
-               [ -f /tmp/.s.PGSQL.5432 ] || rm -f /tmp/.s.PGSQL.5432
-               STARTTEXT="Starting postgresql"
+               [ -e /tmp/.s.PGSQL.5432 ] || rm -f /tmp/.s.PGSQL.5432
+               [ -f /tmp/.s.PGSQL.5432.lock ] || rm -f /tmp/.s.PGSQL.5432.lock
                
                # Check for the PGDATA structure
-               if [ -f $POSTGRES_DATA_DIR/PG_VERSION ] && [ -d $POSTGRES_DATA_DIR/base/template1 ]; then
+               if [ -f $POSTGRES_DATA_DIR/PG_VERSION ] && [ -f $POSTGRES_DATA_DIR/global/pg_control ]; then
                    # Check version of existing PGDATA
-                   if [ `cat $POSTGRES_DATA_DIR/PG_VERSION` != '6.5' ]; then
-                       STARTTEXT="$STARTTEXT [Old version. Need to Upgrade]"
-                       show "$STARTTEXT"; fail
+                   if [ `cat $POSTGRES_DATA_DIR/PG_VERSION` != '7.2' ]; then
+                       show "Databases created under incompatibile version. Migrate them first."; fail
                        exit 1
                    fi
                else
-                   # Create sample database
-                   if [ ! -d $POSTGRES_DATA_DIR ]; then
-                       mkdir -p $POSTGRES_DATA_DIR
-                       chown postgres.postgres $POSTGRES_DATA_DIR
-                   fi
-                   show "Creating postgresql init database"
-                   busy
-                   su postgres -c "LD_LIBRARY_PATH=/usr/lib \
-                       initdb --pgdata=$POSTGRES_DATA_DIR \
-                       --pglib=/usr/lib/pgsql &>/tmp/initdb.pgsql"
-                   deltext; ok
+                   echo "Postgresql database not initialized. Try \"$0 init\" before start."
+                   exit 1
                fi
                
-               show "$STARTTEXT"
+               msg_starting PostgreSQL 
                busy
-               su postgres -c "/usr/bin/postmaster \
-                       $([ "$ALLOW_TCP_CONNECTIONS" = yes ] && echo "-i") \
-                       $([ -n "$MAX_NUM_BACKENDS" ] && echo "-N $MAX_NUM_BACKENDS") \
-                       $([ -n "POSTGRES_$DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
-                       -o \
-                       $([ "$POSTGRES_QUIET" = yes ] && echo "-Q") \
-                       $([ "$POSTGRES_DIABLE_FSYNC" = yes ] && echo "-F") &"
-               sleep 2
-               if ps ax | grep -v grep | grep -q postmaster; then
-                       deltext; ok
+               PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
+                   $(is_yes "$POSTGRES_QUIET" && echo '-s')";
+               POSTMASTER_OPTIONS=" \
+                   $(is_yes "$POSTGRES_QUIET" && echo '-S') \
+                   $(is_yes "$POSTGRES_DISABLE_FSYNC" && echo '-F') \
+                   $(is_yes "$ALLOW_TCP_CONNECTIONS" && echo '-i') \
+                   $(is_yes "$ALLOW_USE_SSL" && echo '-l') \
+                   $([ "$BUFFERS" ] && echo \-B $BUFFERS) \
+                   $([ "$MAXCONN" ] && echo \-N $MAXCONN)";
+
+               TMPDIR=/tmp su postgres -c "\
+                       $(echo "/usr/bin/pg_ctl start -w $PGSQL_CMDLINE -l $POSTGRES_LOG") \
+                       $([ "$POSTMASTER_OPTIONS". != . ] && echo "-o '$POSTMASTER_OPTIONS'") \
+                       $(echo "2>&1 >/dev/null")";
+               
+               out=`status postmaster`
+               if [ $? -eq 0 ]; then 
+                   deltext; ok
                else
-                       deltext; fail
+                   deltext; fail 
+                   exit 1      
                fi
-       else
-               msg_Already_Running Postgresql
        fi
        touch /var/lock/subsys/postgresql
        ;;
   stop)
-       msg_stopping postgresql
-       killproc postmaster
-       sleep 2
-       rm -f /var/run/postmaster.pid
+
+       if [ ! -f /var/lock/subsys/postgresql ]; then
+               msg_not_running PostgreSQL
+               exit 0  
+       fi
+       msg_stopping PostgreSQL
+       busy
+       PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")";
+       TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl stop $PGSQL_CMDLINE -m fast 2>&1 >/dev/null"
+       out=`status postmaster`
+       if [ $? -eq 0 ]; then
+               deltext; fail
+               echo -e "\n$out";
+               exit 1
+       else
+               deltext; ok
+       fi
+       rm -f $POSTGRES_DATA_DIR/postmaster.pid
        rm -f /var/lock/subsys/postgresql
        ;;
   status)
-       status postmaster
+       PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR")";
+       TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl status $PGSQL_CMDLINE"
        ;;
-  restart|reload)
-       $0 stop
+  restart)
+       $0 stop
        $0 start
        ;;
+  reload)
+       if [ ! -f /var/lock/subsys/postgresql ]; then
+               msg_not_running PostgreSQL
+               exit 0  
+       fi
+       msg_reloading PostgreSQL
+       busy
+
+       PGSQL_CMDLINE="$([ -n "$POSTGRES_DATA_DIR" ] && echo "-D $POSTGRES_DATA_DIR") \
+           $([ "$POSTGRES_QUIET". = yes. ] && echo '-s')";
+       POSTMASTER_OPTIONS=" \
+           $([ "$POSTGRES_QUIET". = yes. ] && echo '-S') \
+           $([ "$POSTGRES_DISABLE_FSYNC". = yes. ] && echo '-F') \
+           $([ "$ALLOW_TCP_CONNECTIONS". = yes. ] && echo '-i')";
+
+       TMPDIR=/tmp su postgres -c "\
+               $(echo "/usr/bin/pg_ctl restart $PGSQL_CMDLINE -l $POSTGRES_LOG") \
+               $([ "$POSTMASTER_OPTIONS". != . ] && echo "-o '$POSTMASTER_OPTIONS'") \
+               $(echo "2>&1 >/dev/null")";
+
+       out=`status postmaster`
+       if [ $? -eq 0 ]; then
+               deltext; ok
+       else
+               deltext; fail
+               echo -e "\n$out";
+               exit 1
+       fi
+       ;;
+    init)
+       if [ -f "$POSTGRES_DATA_DIR/PG_VERSION" ]; then
+           echo "Seems that database is initialized now. Remove by hand $POSTGRES_DATA_DIR"
+           echo "before initialize database."
+           exit 1
+       fi
+
+       echo -n "Specify encoding for pgsql database (eg: LATIN2, KOI8) [$ENCODING]: "
+       read new_encoding
+       ENCODING=${new_encoding:-$ENCODING}
+
+       show "Creating initial postgresql database using $ENCODING encoding"
+       started
+
+        # Create sample database
+       if [ ! -d $POSTGRES_DATA_DIR ]; then
+           mkdir -p $POSTGRES_DATA_DIR
+           chown postgres.postgres $POSTGRES_DATA_DIR
+           chmod 700 $POSTGRES_DATA_DIR
+       fi
+
+       
+       TMPDIR=/tmp su - postgres -s /bin/sh -c "LD_LIBRARY_PATH=/usr/lib \
+                       initdb --pgdata=$POSTGRES_DATA_DIR \
+                       -L /usr/share/postgresql -E $ENCODING"
+
+       echo "REMEMBER to setup password for user \"postgres\"!"
+       ;;
   *)
-       msg_Usage "$0 {start|stop|status|restart|reload}"
+       msg_usage "$0 {start|stop|status|restart|reload}"
        exit 1
 esac
 
This page took 0.034562 seconds and 4 git commands to generate.