blob: b64de2c95e96bb0e85e3684b44a13e4ec023c3c6 [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
Dean Troyer41bf4522013-01-28 14:04:39 -06008PG_XTRACE=$(set +o | grep xtrace)
Terry Wilson428af5a2012-11-01 16:12:39 -04009set +o xtrace
10
11register_database postgresql
12
Dean Troyer995eb922013-03-07 16:11:40 -060013# Get rid of everything enough to cleanly change database backends
14function cleanup_database_postgresql {
15 stop_service postgresql
16 if is_ubuntu; then
17 # Get ruthless with mysql
18 sudo aptitude purge -y ~npostgresql
19 return
20 elif is_fedora; then
21 uninstall_package postgresql-server
22 else
23 return
24 fi
25}
26
Terry Wilson428af5a2012-11-01 16:12:39 -040027function recreate_database_postgresql {
28 local db=$1
29 local charset=$2
30 # Avoid unsightly error when calling dropdb when the database doesn't exist
31 psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
32 createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E $charset $db
33}
34
35function configure_database_postgresql {
36 echo_summary "Configuring and starting PostgreSQL"
Vincent Untzb1b04d02012-12-06 11:59:29 +010037 if is_fedora; then
Terry Wilson428af5a2012-11-01 16:12:39 -040038 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
39 PG_CONF=/var/lib/pgsql/data/postgresql.conf
Terry Wilsonadfc7a32012-11-20 13:08:13 -050040 sudo [ -e $PG_HBA ] || sudo postgresql-setup initdb
Vincent Untzb1b04d02012-12-06 11:59:29 +010041 elif is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040042 PG_DIR=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
43 PG_HBA=$PG_DIR/pg_hba.conf
44 PG_CONF=$PG_DIR/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010045 elif is_suse; then
46 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
47 PG_CONF=/var/lib/pgsql/data/postgresql.conf
48 # initdb is called when postgresql is first started
49 sudo [ -e $PG_HBA ] || start_service postgresql
50 else
51 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040052 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040053 # Listen on all addresses
54 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $PG_CONF
55 # Do password auth from all IPv4 clients
56 sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $PG_HBA
57 # Do password auth for all IPv6 clients
58 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 +010059 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040060
61 # If creating the role fails, chances are it already existed. Try to alter it.
Dean Troyerc1b486a2012-11-05 14:26:09 -060062 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" || \
63 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 -040064}
65
66function install_database_postgresql {
67 echo_summary "Installing postgresql"
68 PGPASS=$HOME/.pgpass
69 if [[ ! -e $PGPASS ]]; then
70 cat <<EOF > $PGPASS
71*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
72EOF
73 chmod 0600 $PGPASS
74 else
75 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $PGPASS
76 fi
Vincent Untz00011c02012-12-06 09:56:32 +010077 if is_ubuntu; then
78 install_package postgresql
79 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -040080 install_package postgresql-server
81 else
Vincent Untz00011c02012-12-06 09:56:32 +010082 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -040083 fi
84}
85
86function database_connection_url_postgresql {
Attila Fazekas7e79d912013-03-03 12:23:04 +010087 local db=$1
88 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -040089}
90
91# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -060092$PG_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -040093
94# Local variables:
95# mode: shell-script
96# End: