5 # Manage the PostgreSQL database server
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16 local_config=/etc/planetlab/configs/site.xml
22 PGDATA=/var/lib/pgsql/data
23 postgresql_conf=$PGDATA/postgresql.conf
24 pghba_conf=$PGDATA/pg_hba.conf
26 # Export so that we do not have to specify -p to psql invocations
27 export PGPORT=$PLC_DB_PORT
29 # /etc/init.d/postgresql always returns 0, even on failure
32 # start() always returns 0
33 (exec 3>&- 4>&- ; service postgresql start)
35 # status() will still return 0 even while still initializing
36 if status postmaster && [ -f /var/lock/subsys/postgresql ] ; then
37 # The only way we can be sure is if we can access it
38 for i in $(seq 1 10) ; do
39 # Must do this as the postgres user initially (before we
40 # fix pg_hba.conf to passwordless localhost access).
41 su -c 'psql -U postgres -c "" template1' postgres && return 0
51 service postgresql initdb &> /dev/null || :
57 if [ "$PLC_DB_ENABLED" != "1" ] ; then
61 MESSAGE=$"Starting PostgreSQL server"
64 # Set data directory and redirect startup output to /var/log/pgsql
65 mkdir -p /etc/sysconfig/pgsql
68 echo "PGLOG=/var/log/pgsql"
69 echo "PGPORT=$PLC_DB_PORT"
70 ) >>/etc/sysconfig/pgsql/postgresql
72 # Fix ownership (rpm installation may have changed it)
73 chown -R -H postgres:postgres $(dirname $PGDATA)
75 # PostgreSQL must be started at least once to bootstrap
77 if [ ! -f $postgresql_conf ] ; then
80 service postgresql stop
84 # Enable DB server. PostgreSQL >=8.0 defines listen_addresses,
85 # PostgreSQL 7.x uses tcpip_socket.
86 if grep -q listen_addresses $postgresql_conf ; then
87 sed -i -e '/^listen_addresses/d' $postgresql_conf
88 echo "listen_addresses = '*'" >>$postgresql_conf
89 elif grep -q tcpip_socket $postgresql_conf ; then
90 sed -i -e '/^tcpip_socket/d' $postgresql_conf
91 echo "tcpip_socket = true" >>$postgresql_conf
94 # Disable access to all DBs from all hosts
95 sed -i -e '/^\(host\|local\)/d' $pghba_conf
97 # Enable passwordless localhost access
98 echo "local all all trust" >>$pghba_conf
100 # Enable access from the API, boot, and web servers
101 PLC_API_IP=$(gethostbyname $PLC_API_HOST)
102 PLC_BOOT_IP=$(gethostbyname $PLC_BOOT_HOST)
103 PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
105 if [ -z "$PLC_API_IP" ] ; then
106 MESSAGE=$"PLC_API_IP is not set"
110 if [ -z "$PLC_BOOT_IP" ] ; then
111 MESSAGE=$"PLC_BOOT_IP is not set"
115 if [ -z "$PLC_WWW_IP" ] ; then
116 MESSAGE=$"PLC_WWW_IP is not set"
120 if [ $ip_failure -eq 1 ] ; then
126 echo "host $PLC_DB_NAME $PLC_DB_USER 127.0.0.1/32 password"
127 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
128 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_BOOT_IP/32 password"
129 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
130 # Drupal also uses PostgreSQL
131 echo "host drupal $PLC_DB_USER 127.0.0.1/32 password"
132 echo "host drupal $PLC_DB_USER $PLC_WWW_IP/32 password"
135 # Append site-specific access rules
136 for file in $pghba_conf.d/*.conf ; do
137 cat "$file" >>$pghba_conf
140 # Fix ownership (sed -i changes it)
141 chown postgres:postgres $postgresql_conf $pghba_conf
143 # Start up the server
147 # Create/update the unprivileged database user and password
148 if [ -z "$PLC_DB_PASSWORD" ] ; then
149 PLC_DB_PASSWORD=$(uuidgen)
150 plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save=$local_config $local_config
153 if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
154 psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
156 psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
160 # Create the databases if necessary
161 if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
162 createdb -U postgres --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
163 psql -U $PLC_DB_USER -f /usr/share/plc_api/$PLC_DB_NAME.sql $PLC_DB_NAME
166 if ! psql -U $PLC_DB_USER -c "" drupal >/dev/null 2>&1 ; then
167 createdb -U postgres --encoding=UNICODE --owner=$PLC_DB_USER drupal
168 psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal
176 MESSAGE=$"Stopping PostgreSQL server"
179 # Drop the current user in case the username changes
180 psql -U postgres -c "DROP USER $PLC_DB_USER" template1
182 # WARNING: If the DB name changes, the old DB will be left
183 # intact and a new one will be created. If it changes
184 # back, the old DB will not be re-created.
186 # Shut down the server
187 service postgresql stop
189 # /etc/init.d/postgresql fails if it is not running
190 [ "$PLC_DB_ENABLED" = 1 ] && check