5 # Manage the PostgreSQL database server
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
10 # $Id: guest.init,v 1.12 2006/04/04 22:09:47 mlhuang Exp $
13 # Source function library and configuration
14 . /etc/plc.d/functions
17 PGDATA=/var/lib/pgsql/data
18 postgresql_conf=$PGDATA/postgresql.conf
19 pghba_conf=$PGDATA/pg_hba.conf
21 # Export so that we do not have to specify -p to psql invocations
22 export PGPORT=$PLC_DB_PORT
24 # /etc/init.d/postgresql always returns 0, even on failure
27 service postgresql start
28 status postmaster && [ -f /var/lock/subsys/postgresql ]
33 if [ "$PLC_DB_ENABLED" != "1" ] ; then
37 MESSAGE=$"Starting database server"
40 # Set data directory and redirect startup output to /var/log/pgsql
41 mkdir -p /etc/sysconfig/pgsql
44 echo "PGLOG=/var/log/pgsql"
45 echo "PGPORT=$PLC_DB_PORT"
46 ) >>/etc/sysconfig/pgsql/postgresql
48 # Fix ownership (rpm installation may have changed it)
49 chown -R -H postgres:postgres $(dirname $PGDATA)
51 # PostgreSQL must be started at least once to bootstrap
53 if [ ! -f $postgresql_conf ] ; then
56 service postgresql stop
60 # Enable DB server. PostgreSQL >=8.0 defines listen_addresses,
61 # PostgreSQL 7.x uses tcpip_socket.
62 if grep -q listen_addresses $postgresql_conf ; then
63 sed -i -e '/^listen_addresses/d' $postgresql_conf
64 echo "listen_addresses = '*'" >>$postgresql_conf
65 elif grep -q tcpip_socket $postgresql_conf ; then
66 sed -i -e '/^tcpip_socket/d' $postgresql_conf
67 echo "tcpip_socket = true" >>$postgresql_conf
70 # Disable access to all DBs from all hosts
71 sed -i -e '/^\(host\|local\)/d' $pghba_conf
73 # Enable passwordless localhost access
74 echo "local all all trust" >>$pghba_conf
76 # Enable access from the API and web servers
77 PLC_API_IP=$(gethostbyname $PLC_API_HOST)
78 PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
80 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
81 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
84 # Fix ownership (sed -i changes it)
85 chown postgres:postgres $postgresql_conf $pghba_conf
91 # Create/update the unprivileged database user and password
92 if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
93 psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
95 psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
98 # Create the database if necessary
99 if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
100 createdb -U postgres $PLC_DB_NAME
101 psql -U $PLC_DB_USER -f /usr/share/pl_db/plc_schema_3.sql $PLC_DB_NAME
108 MESSAGE=$"Stopping database server"
111 # Drop the current user in case the username changes
112 psql -U postgres -c "DROP USER $PLC_DB_USER" template1
114 # WARNING: If the DB name changes, the old DB will be left
115 # intact and a new one will be created. If it changes
116 # back, the old DB will not be re-created.
118 # Shut down the server
119 service postgresql stop