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
23 postgresql_sysconfig=/etc/sysconfig/pgsql/postgresql
25 # Export so that we do not have to specify -p to psql invocations
26 export PGPORT=$PLC_DB_PORT
28 # can't trust the return of service postgresql start / nor status
29 function postgresql_check () {
31 # wait until postmaster is up and running - or 10s max
32 if status postmaster && [ -f /var/lock/subsys/postgresql ] ; then
33 # The only way we can be sure is if we can access it
34 for i in $(seq 1 10) ; do
35 # Must do this as the postgres user initially (before we
36 # fix pg_hba.conf to passwordless localhost access).
37 su -c 'psql -U postgres -c "" template1' postgres && return 0
47 if [ "$PLC_DB_ENABLED" != "1" ] ; then
51 MESSAGE=$"Starting PostgreSQL server"
55 # xxx on f16, the systemd init script won't read /etc/sysconfig/pgsql/postgresql any more
56 # need to find out how to perform this configuration, if still needed
57 # Set data directory and redirect startup output to /var/log/pgsql
58 mkdir -p $(dirname $postgresql_sysconfig)
59 touch $postgresql_sysconfig
60 tmp=${postgresql_sysconfig}.new
61 # remove any previous definitions and write ours
62 ( egrep -v '^(PGDATA=|PGLOG=|PGPORT=)' $postgresql_sysconfig
64 echo "PGLOG=/var/log/pgsql"
65 echo "PGPORT=$PLC_DB_PORT"
66 ) > $tmp ; mv -f $tmp $postgresql_sysconfig
68 ######## /var/lib/pgsql/data
69 # Fix ownership of /var/lib/pgsql (rpm installation may have changed it)
70 chown -R -H postgres:postgres $(dirname $PGDATA)
72 # PostgreSQL must be started at least once to bootstrap
74 if [ ! -f $postgresql_conf ] ; then
75 # fedora 16 uses systemd
76 # http://docs.fedoraproject.org/en-US/Fedora/16/html/Release_Notes/sect-Release_Notes-Changes_for_Sysadmin.html
77 if type postgresql-setup >& /dev/null ; then
78 postgresql-setup initdb || :
81 service postgresql initdb &> /dev/null || postgresql :
86 ######## /var/lib/pgsql/data/postgresql.conf
87 # Enable DB server. drop Postgresql<=7.x
88 # PostgreSQL >=8.0 defines listen_addresses
89 # listen on a specific IP + localhost, more robust when run within a vserver
90 sed -i -e '/^listen_addresses/d' $postgresql_conf
91 echo "listen_addresses = '${PLC_DB_HOST},localhost'" >> $postgresql_conf
92 # tweak timezone to be 'UTC'
93 sed -i -e '/^timezone=/d' $postgresql_conf
94 echo "timezone='UTC'" >> $postgresql_conf
96 ######## /var/lib/pgsql/data/pg_hba.conf
97 # Disable access to MyPLC and drupal DBs from all hosts
98 sed -i -e '/^\(host\|local\)/d' $pghba_conf
100 # Enable passwordless localhost access
101 echo "local all all trust" >>$pghba_conf
103 # Enable access from the API, boot, and web servers
104 PLC_API_IP=$(gethostbyname $PLC_API_HOST)
105 PLC_BOOT_IP=$(gethostbyname $PLC_BOOT_HOST)
106 PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
108 if [ -z "$PLC_API_IP" ] ; then
109 MESSAGE=$"PLC_API_IP is not set"
113 if [ -z "$PLC_BOOT_IP" ] ; then
114 MESSAGE=$"PLC_BOOT_IP is not set"
118 if [ -z "$PLC_WWW_IP" ] ; then
119 MESSAGE=$"PLC_WWW_IP is not set"
123 if [ $ip_failure -eq 1 ] ; then
129 echo "host $PLC_DB_NAME $PLC_DB_USER 127.0.0.1/32 password"
130 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
131 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_BOOT_IP/32 password"
132 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
133 # Drupal also uses PostgreSQL
134 echo "host drupal $PLC_DB_USER 127.0.0.1/32 password"
135 echo "host drupal $PLC_DB_USER $PLC_WWW_IP/32 password"
138 # Append site-specific access rules
139 for file in $pghba_conf.d/*.conf ; do
140 cat "$file" >>$pghba_conf
143 # Fix ownership (sed -i changes it)
144 chown postgres:postgres $postgresql_conf $pghba_conf
146 ######## Start up the server - ignore retcod and check this our way
147 (exec 3>&- 4>&- ; service postgresql start)
151 ######## Create/update the unprivileged database user and password
152 if [ -z "$PLC_DB_PASSWORD" ] ; then
153 PLC_DB_PASSWORD=$(uuidgen)
154 plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save=$local_config $local_config
158 if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
159 psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
161 psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
165 ######## Create the databases if necessary
166 if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
167 createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
168 psql -U $PLC_DB_USER -f /usr/share/plc_api/$PLC_DB_NAME.sql $PLC_DB_NAME
171 if ! psql -U $PLC_DB_USER -c "" drupal >/dev/null 2>&1 ; then
172 createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER drupal
173 psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal
181 MESSAGE=$"Stopping PostgreSQL server"
184 # Drop the current user in case the username changes
185 psql -U postgres -c "DROP USER $PLC_DB_USER" template1
187 # WARNING: If the DB name changes, the old DB will be left
188 # intact and a new one will be created. If it changes
189 # back, the old DB will not be re-created.
191 # Shut down the server
192 service postgresql stop
194 # /etc/init.d/postgresql fails if it is not running
195 [ "$PLC_DB_ENABLED" = 1 ] && check