blob: ee24c8b5e9a67bab57dbeed862497998dff3b82e [file] [log] [blame]
Terry Wilson428af5a2012-11-01 16:12:39 -04001# lib/postgresql
2# Functions to control the configuration and operation of the PostgreSQL database backend
3
4# Dependencies:
5# DATABASE_{HOST,USER,PASSWORD} must be defined
6
7# Save trace setting
8XTRACE=$(set +o | grep xtrace)
9set +o xtrace
10
11register_database postgresql
12
13function recreate_database_postgresql {
14 local db=$1
15 local charset=$2
16 # Avoid unsightly error when calling dropdb when the database doesn't exist
17 psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
18 createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E $charset $db
19}
20
21function configure_database_postgresql {
22 echo_summary "Configuring and starting PostgreSQL"
23 if [[ "$os_PACKAGE" = "rpm" ]]; then
24 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
25 PG_CONF=/var/lib/pgsql/data/postgresql.conf
26 else
27 PG_DIR=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
28 PG_HBA=$PG_DIR/pg_hba.conf
29 PG_CONF=$PG_DIR/postgresql.conf
30 fi
31 sudo [ -e /var/lib/pgsql/data ] || sudo postgresql-setup initdb
32 # Listen on all addresses
33 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $PG_CONF
34 # Do password auth from all IPv4 clients
35 sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $PG_HBA
36 # Do password auth for all IPv6 clients
37 sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $PG_HBA
38 start_service postgresql
39
40 # If creating the role fails, chances are it already existed. Try to alter it.
Dean Troyerc1b486a2012-11-05 14:26:09 -060041 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" || \
42 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
Terry Wilson428af5a2012-11-01 16:12:39 -040043}
44
45function install_database_postgresql {
46 echo_summary "Installing postgresql"
47 PGPASS=$HOME/.pgpass
48 if [[ ! -e $PGPASS ]]; then
49 cat <<EOF > $PGPASS
50*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
51EOF
52 chmod 0600 $PGPASS
53 else
54 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $PGPASS
55 fi
56 if [[ "$os_PACKAGE" = "rpm" ]]; then
57 install_package postgresql-server
58 else
59 install_package postgresql
60 fi
61}
62
63function database_connection_url_postgresql {
64 local output=$1
65 local db=$2
66 eval "$output=$BASE_SQL_CONN/$db?client_encoding=utf8"
67}
68
69# Restore xtrace
70$XTRACE