blob: 317e0ebf538cbbd32c5e8ff8c31ea844f675ab75 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyer6d04fd72012-12-21 11:03:37 -06003# lib/databases/postgresql
4# Functions to control the configuration and operation of the **PostgreSQL** database backend
Terry Wilson428af5a2012-11-01 16:12:39 -04005
6# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - DATABASE_{HOST,USER,PASSWORD} must be defined
Terry Wilson428af5a2012-11-01 16:12:39 -04009
10# Save trace setting
Dean Troyer41bf4522013-01-28 14:04:39 -060011PG_XTRACE=$(set +o | grep xtrace)
Terry Wilson428af5a2012-11-01 16:12:39 -040012set +o xtrace
13
Dean Troyercc6b4432013-04-08 15:38:03 -050014
Matt Riedemann94c654e2014-07-09 12:38:36 -070015MAX_DB_CONNECTIONS=${MAX_DB_CONNECTIONS:-200}
16
17
Terry Wilson428af5a2012-11-01 16:12:39 -040018register_database postgresql
19
Dean Troyercc6b4432013-04-08 15:38:03 -050020
21# Functions
22# ---------
23
Dean Troyer995eb922013-03-07 16:11:40 -060024# Get rid of everything enough to cleanly change database backends
25function cleanup_database_postgresql {
26 stop_service postgresql
27 if is_ubuntu; then
28 # Get ruthless with mysql
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010029 apt_get purge -y postgresql*
Dean Troyer995eb922013-03-07 16:11:40 -060030 return
Thomas Bechtolda6509012014-06-23 13:47:36 +020031 elif is_fedora || is_suse; then
Dean Troyer995eb922013-03-07 16:11:40 -060032 uninstall_package postgresql-server
33 else
34 return
35 fi
36}
37
Terry Wilson428af5a2012-11-01 16:12:39 -040038function recreate_database_postgresql {
39 local db=$1
Terry Wilson428af5a2012-11-01 16:12:39 -040040 # Avoid unsightly error when calling dropdb when the database doesn't exist
41 psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020042 createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E utf8 $db
Terry Wilson428af5a2012-11-01 16:12:39 -040043}
44
45function configure_database_postgresql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050046 local pg_conf pg_dir pg_hba root_roles
Terry Wilson428af5a2012-11-01 16:12:39 -040047 echo_summary "Configuring and starting PostgreSQL"
Vincent Untzb1b04d02012-12-06 11:59:29 +010048 if is_fedora; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050049 pg_hba=/var/lib/pgsql/data/pg_hba.conf
50 pg_conf=/var/lib/pgsql/data/postgresql.conf
51 if ! sudo [ -e $pg_hba ]; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010052 sudo postgresql-setup initdb
Attila Fazekas315f7b02014-01-27 09:40:29 +010053 fi
Vincent Untzb1b04d02012-12-06 11:59:29 +010054 elif is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050055 pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
56 pg_hba=$pg_dir/pg_hba.conf
57 pg_conf=$pg_dir/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010058 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050059 pg_hba=/var/lib/pgsql/data/pg_hba.conf
60 pg_conf=/var/lib/pgsql/data/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010061 # initdb is called when postgresql is first started
Dean Troyer3ef23bc2014-07-25 14:56:22 -050062 sudo [ -e $pg_hba ] || start_service postgresql
Vincent Untzb1b04d02012-12-06 11:59:29 +010063 else
64 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040065 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040066 # Listen on all addresses
Dean Troyer3ef23bc2014-07-25 14:56:22 -050067 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf
Matt Riedemann94c654e2014-07-09 12:38:36 -070068 # Set max_connections
Dean Troyer3ef23bc2014-07-25 14:56:22 -050069 sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf
Terry Wilson428af5a2012-11-01 16:12:39 -040070 # Do password auth from all IPv4 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050071 sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $pg_hba
Terry Wilson428af5a2012-11-01 16:12:39 -040072 # Do password auth for all IPv6 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050073 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 +010074 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040075
Chmouel Boudjnah00b43412014-01-02 10:33:21 +000076 # Create the role if it's not here or else alter it.
77 root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
78 if [[ ${root_roles} == *HERE ]];then
79 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
80 else
81 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
82 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040083}
84
85function install_database_postgresql {
86 echo_summary "Installing postgresql"
Dean Troyer3ef23bc2014-07-25 14:56:22 -050087 local pgpass=$HOME/.pgpass
88 if [[ ! -e $pgpass ]]; then
89 cat <<EOF > $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040090*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
91EOF
Dean Troyer3ef23bc2014-07-25 14:56:22 -050092 chmod 0600 $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040093 else
Dean Troyer3ef23bc2014-07-25 14:56:22 -050094 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040095 fi
Vincent Untz00011c02012-12-06 09:56:32 +010096 if is_ubuntu; then
97 install_package postgresql
98 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -040099 install_package postgresql-server
100 else
Vincent Untz00011c02012-12-06 09:56:32 +0100101 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -0400102 fi
103}
104
105function database_connection_url_postgresql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100106 local db=$1
107 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400108}
109
Dean Troyercc6b4432013-04-08 15:38:03 -0500110
Terry Wilson428af5a2012-11-01 16:12:39 -0400111# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600112$PG_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400113
114# Local variables:
115# mode: shell-script
116# End: