5 # Manage the PostgreSQL database server
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
11 # Source function library and configuration
12 . /etc/plc.d/functions
13 . /etc/planetlab/plc_config
14 local_config=/etc/planetlab/configs/site.xml
20 PGDATA=/var/lib/pgsql/data
21 postgresql_conf=$PGDATA/postgresql.conf
22 pghba_conf=$PGDATA/pg_hba.conf
24 # Export so that we do not have to specify -p to psql invocations
25 export PGPORT=$PLC_DB_PORT
27 # /etc/init.d/postgresql always returns 0, even on failure
30 # start() always returns 0
31 (exec 3>&- 4>&- ; service postgresql start)
33 # status() will still return 0 even while still initializing
34 if status postmaster && [ -f /var/lock/subsys/postgresql ] ; then
35 # The only way we can be sure is if we can access it
36 for i in $(seq 1 10) ; do
37 # Must do this as the postgres user initially (before we
38 # fix pg_hba.conf to passwordless localhost access).
39 su -c 'psql -U postgres -c "" template1' postgres && return 0
49 service postgresql initdb &> /dev/null || :
55 if [ "$PLC_DB_ENABLED" != "1" ] ; then
59 MESSAGE=$"Starting PostgreSQL server"
62 # Set data directory and redirect startup output to /var/log/pgsql
63 mkdir -p /etc/sysconfig/pgsql
66 echo "PGLOG=/var/log/pgsql"
67 echo "PGPORT=$PLC_DB_PORT"
68 ) >>/etc/sysconfig/pgsql/postgresql
70 # Fix ownership (rpm installation may have changed it)
71 chown -R -H postgres:postgres $(dirname $PGDATA)
73 # PostgreSQL must be started at least once to bootstrap
75 if [ ! -f $postgresql_conf ] ; then
78 service postgresql stop
82 # Enable DB server. drop Postgresql<=7.x
83 # PostgreSQL >=8.0 defines listen_addresses
84 # listen on a specific IP + localhost, more robust when run within a vserver
85 sed -i -e '/^listen_addresses/d' $postgresql_conf
86 echo "listen_addresses = '${PLC_DB_HOST},localhost'" >> $postgresql_conf
87 # tweak timezone to be 'UTC'
88 sed -i -e '/^timezone=/d' $postgresql_conf
89 echo "timezone='UTC'" >> $postgresql_conf
91 # Disable access to all DBs from all hosts
92 sed -i -e '/^\(host\|local\)/d' $pghba_conf
94 # Enable passwordless localhost access
95 echo "local all all trust" >>$pghba_conf
97 # Enable access from the API, boot, and web servers
98 PLC_API_IP=$(gethostbyname $PLC_API_HOST)
99 PLC_BOOT_IP=$(gethostbyname $PLC_BOOT_HOST)
100 PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
102 if [ -z "$PLC_API_IP" ] ; then
103 MESSAGE=$"PLC_API_IP is not set"
107 if [ -z "$PLC_BOOT_IP" ] ; then
108 MESSAGE=$"PLC_BOOT_IP is not set"
112 if [ -z "$PLC_WWW_IP" ] ; then
113 MESSAGE=$"PLC_WWW_IP is not set"
117 if [ $ip_failure -eq 1 ] ; then
123 echo "host $PLC_DB_NAME $PLC_DB_USER 127.0.0.1/32 password"
124 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
125 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_BOOT_IP/32 password"
126 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
127 # Drupal also uses PostgreSQL
128 echo "host drupal $PLC_DB_USER 127.0.0.1/32 password"
129 echo "host drupal $PLC_DB_USER $PLC_WWW_IP/32 password"
132 # Append site-specific access rules
133 for file in $pghba_conf.d/*.conf ; do
134 cat "$file" >>$pghba_conf
137 # Fix ownership (sed -i changes it)
138 chown postgres:postgres $postgresql_conf $pghba_conf
140 # Start up the server
144 # Create/update the unprivileged database user and password
145 if [ -z "$PLC_DB_PASSWORD" ] ; then
146 PLC_DB_PASSWORD=$(uuidgen)
147 plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save=$local_config $local_config
150 if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
151 psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
153 psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
157 # Create the databases if necessary
158 if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
159 createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
160 psql -U $PLC_DB_USER -f /usr/share/plc_api/$PLC_DB_NAME.sql $PLC_DB_NAME
163 if ! psql -U $PLC_DB_USER -c "" drupal >/dev/null 2>&1 ; then
164 createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER drupal
165 psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal
173 MESSAGE=$"Stopping PostgreSQL server"
176 # Drop the current user in case the username changes
177 psql -U postgres -c "DROP USER $PLC_DB_USER" template1
179 # WARNING: If the DB name changes, the old DB will be left
180 # intact and a new one will be created. If it changes
181 # back, the old DB will not be re-created.
183 # Shut down the server
184 service postgresql stop
186 # /etc/init.d/postgresql fails if it is not running
187 [ "$PLC_DB_ENABLED" = 1 ] && check