blob: 10ab7219315e57c1fc6bba2a1e5913f473c6f8bc [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
Terry Wilson428af5a2012-11-01 16:12:39 -040031 # Listen on all addresses
32 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $PG_CONF
33 # Do password auth from all IPv4 clients
34 sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $PG_HBA
35 # Do password auth for all IPv6 clients
36 sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $PG_HBA
37 start_service postgresql
38
39 # If creating the role fails, chances are it already existed. Try to alter it.
Dean Troyerc1b486a2012-11-05 14:26:09 -060040 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" || \
41 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 -040042}
43
44function install_database_postgresql {
45 echo_summary "Installing postgresql"
46 PGPASS=$HOME/.pgpass
47 if [[ ! -e $PGPASS ]]; then
48 cat <<EOF > $PGPASS
49*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
50EOF
51 chmod 0600 $PGPASS
52 else
53 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $PGPASS
54 fi
55 if [[ "$os_PACKAGE" = "rpm" ]]; then
56 install_package postgresql-server
57 else
58 install_package postgresql
59 fi
60}
61
62function database_connection_url_postgresql {
63 local output=$1
64 local db=$2
65 eval "$output=$BASE_SQL_CONN/$db?client_encoding=utf8"
66}
67
68# Restore xtrace
69$XTRACE