blob: fb6d304180c92ff4ff55b1f42d8bd41ff8a23f1e [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:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# - DATABASE_{HOST,USER,PASSWORD} must be defined
Terry Wilson428af5a2012-11-01 16:12:39 -04007
8# Save trace setting
Dean Troyer41bf4522013-01-28 14:04:39 -06009PG_XTRACE=$(set +o | grep xtrace)
Terry Wilson428af5a2012-11-01 16:12:39 -040010set +o xtrace
11
Dean Troyercc6b4432013-04-08 15:38:03 -050012
Matt Riedemann94c654e2014-07-09 12:38:36 -070013MAX_DB_CONNECTIONS=${MAX_DB_CONNECTIONS:-200}
14
15
Terry Wilson428af5a2012-11-01 16:12:39 -040016register_database postgresql
17
Dean Troyercc6b4432013-04-08 15:38:03 -050018
19# Functions
20# ---------
21
Dean Troyer995eb922013-03-07 16:11:40 -060022# Get rid of everything enough to cleanly change database backends
23function cleanup_database_postgresql {
24 stop_service postgresql
25 if is_ubuntu; then
26 # Get ruthless with mysql
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010027 apt_get purge -y postgresql*
Dean Troyer995eb922013-03-07 16:11:40 -060028 return
Thomas Bechtolda6509012014-06-23 13:47:36 +020029 elif is_fedora || is_suse; then
Dean Troyer995eb922013-03-07 16:11:40 -060030 uninstall_package postgresql-server
31 else
32 return
33 fi
34}
35
Terry Wilson428af5a2012-11-01 16:12:39 -040036function recreate_database_postgresql {
37 local db=$1
38 local charset=$2
39 # Avoid unsightly error when calling dropdb when the database doesn't exist
40 psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
41 createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E $charset $db
42}
43
44function configure_database_postgresql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050045 local pg_conf pg_dir pg_hba root_roles
Terry Wilson428af5a2012-11-01 16:12:39 -040046 echo_summary "Configuring and starting PostgreSQL"
Vincent Untzb1b04d02012-12-06 11:59:29 +010047 if is_fedora; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050048 pg_hba=/var/lib/pgsql/data/pg_hba.conf
49 pg_conf=/var/lib/pgsql/data/postgresql.conf
50 if ! sudo [ -e $pg_hba ]; then
Attila Fazekas315f7b02014-01-27 09:40:29 +010051 if ! [[ $DISTRO =~ (rhel6) ]]; then
52 sudo postgresql-setup initdb
53 else
54 sudo service postgresql initdb
55 fi
56 fi
Vincent Untzb1b04d02012-12-06 11:59:29 +010057 elif is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050058 pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
59 pg_hba=$pg_dir/pg_hba.conf
60 pg_conf=$pg_dir/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010061 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050062 pg_hba=/var/lib/pgsql/data/pg_hba.conf
63 pg_conf=/var/lib/pgsql/data/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010064 # initdb is called when postgresql is first started
Dean Troyer3ef23bc2014-07-25 14:56:22 -050065 sudo [ -e $pg_hba ] || start_service postgresql
Vincent Untzb1b04d02012-12-06 11:59:29 +010066 else
67 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040068 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040069 # Listen on all addresses
Dean Troyer3ef23bc2014-07-25 14:56:22 -050070 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf
Matt Riedemann94c654e2014-07-09 12:38:36 -070071 # Set max_connections
Dean Troyer3ef23bc2014-07-25 14:56:22 -050072 sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf
Terry Wilson428af5a2012-11-01 16:12:39 -040073 # Do password auth from all IPv4 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050074 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 -040075 # Do password auth for all IPv6 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050076 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 +010077 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040078
Chmouel Boudjnah00b43412014-01-02 10:33:21 +000079 # Create the role if it's not here or else alter it.
80 root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
81 if [[ ${root_roles} == *HERE ]];then
82 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
83 else
84 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
85 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040086}
87
88function install_database_postgresql {
89 echo_summary "Installing postgresql"
Dean Troyer3ef23bc2014-07-25 14:56:22 -050090 local pgpass=$HOME/.pgpass
91 if [[ ! -e $pgpass ]]; then
92 cat <<EOF > $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040093*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
94EOF
Dean Troyer3ef23bc2014-07-25 14:56:22 -050095 chmod 0600 $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040096 else
Dean Troyer3ef23bc2014-07-25 14:56:22 -050097 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040098 fi
Vincent Untz00011c02012-12-06 09:56:32 +010099 if is_ubuntu; then
100 install_package postgresql
101 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400102 install_package postgresql-server
103 else
Vincent Untz00011c02012-12-06 09:56:32 +0100104 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -0400105 fi
106}
107
108function database_connection_url_postgresql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100109 local db=$1
110 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400111}
112
Dean Troyercc6b4432013-04-08 15:38:03 -0500113
Terry Wilson428af5a2012-11-01 16:12:39 -0400114# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600115$PG_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400116
117# Local variables:
118# mode: shell-script
119# End: