blob: 04db714afad8b6570fae89166ec0ffa45e33475e [file] [log] [blame]
Dean Troyer6d04fd72012-12-21 11:03:37 -06001# lib/databases/postgresql
2# Functions to control the configuration and operation of the **PostgreSQL** database backend
Terry Wilson428af5a2012-11-01 16:12:39 -04003
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"
Vincent Untzb1b04d02012-12-06 11:59:29 +010023 if is_fedora; then
Terry Wilson428af5a2012-11-01 16:12:39 -040024 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
25 PG_CONF=/var/lib/pgsql/data/postgresql.conf
Terry Wilsonadfc7a32012-11-20 13:08:13 -050026 sudo [ -e $PG_HBA ] || sudo postgresql-setup initdb
Vincent Untzb1b04d02012-12-06 11:59:29 +010027 elif is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040028 PG_DIR=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
29 PG_HBA=$PG_DIR/pg_hba.conf
30 PG_CONF=$PG_DIR/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010031 elif is_suse; then
32 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
33 PG_CONF=/var/lib/pgsql/data/postgresql.conf
34 # initdb is called when postgresql is first started
35 sudo [ -e $PG_HBA ] || start_service postgresql
36 else
37 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040038 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040039 # Listen on all addresses
40 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $PG_CONF
41 # Do password auth from all IPv4 clients
42 sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $PG_HBA
43 # Do password auth for all IPv6 clients
44 sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $PG_HBA
Vincent Untzb1b04d02012-12-06 11:59:29 +010045 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040046
47 # If creating the role fails, chances are it already existed. Try to alter it.
Dean Troyerc1b486a2012-11-05 14:26:09 -060048 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" || \
49 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 -040050}
51
52function install_database_postgresql {
53 echo_summary "Installing postgresql"
54 PGPASS=$HOME/.pgpass
55 if [[ ! -e $PGPASS ]]; then
56 cat <<EOF > $PGPASS
57*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
58EOF
59 chmod 0600 $PGPASS
60 else
61 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $PGPASS
62 fi
Vincent Untz00011c02012-12-06 09:56:32 +010063 if is_ubuntu; then
64 install_package postgresql
65 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -040066 install_package postgresql-server
67 else
Vincent Untz00011c02012-12-06 09:56:32 +010068 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -040069 fi
70}
71
72function database_connection_url_postgresql {
73 local output=$1
74 local db=$2
75 eval "$output=$BASE_SQL_CONN/$db?client_encoding=utf8"
76}
77
78# Restore xtrace
79$XTRACE