blob: 76491c479dd8754c0a18fd67fff3e447e529033f [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
40 local charset=$2
41 # Avoid unsightly error when calling dropdb when the database doesn't exist
42 psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
43 createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E $charset $db
44}
45
46function configure_database_postgresql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050047 local pg_conf pg_dir pg_hba root_roles
Terry Wilson428af5a2012-11-01 16:12:39 -040048 echo_summary "Configuring and starting PostgreSQL"
Vincent Untzb1b04d02012-12-06 11:59:29 +010049 if is_fedora; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050050 pg_hba=/var/lib/pgsql/data/pg_hba.conf
51 pg_conf=/var/lib/pgsql/data/postgresql.conf
52 if ! sudo [ -e $pg_hba ]; then
Attila Fazekas315f7b02014-01-27 09:40:29 +010053 if ! [[ $DISTRO =~ (rhel6) ]]; then
54 sudo postgresql-setup initdb
55 else
56 sudo service postgresql initdb
57 fi
58 fi
Vincent Untzb1b04d02012-12-06 11:59:29 +010059 elif is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050060 pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
61 pg_hba=$pg_dir/pg_hba.conf
62 pg_conf=$pg_dir/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010063 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050064 pg_hba=/var/lib/pgsql/data/pg_hba.conf
65 pg_conf=/var/lib/pgsql/data/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010066 # initdb is called when postgresql is first started
Dean Troyer3ef23bc2014-07-25 14:56:22 -050067 sudo [ -e $pg_hba ] || start_service postgresql
Vincent Untzb1b04d02012-12-06 11:59:29 +010068 else
69 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040070 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040071 # Listen on all addresses
Dean Troyer3ef23bc2014-07-25 14:56:22 -050072 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf
Matt Riedemann94c654e2014-07-09 12:38:36 -070073 # Set max_connections
Dean Troyer3ef23bc2014-07-25 14:56:22 -050074 sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf
Terry Wilson428af5a2012-11-01 16:12:39 -040075 # Do password auth from all IPv4 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050076 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 -040077 # Do password auth for all IPv6 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050078 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 +010079 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040080
Chmouel Boudjnah00b43412014-01-02 10:33:21 +000081 # Create the role if it's not here or else alter it.
82 root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
83 if [[ ${root_roles} == *HERE ]];then
84 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
85 else
86 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
87 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040088}
89
90function install_database_postgresql {
91 echo_summary "Installing postgresql"
Dean Troyer3ef23bc2014-07-25 14:56:22 -050092 local pgpass=$HOME/.pgpass
93 if [[ ! -e $pgpass ]]; then
94 cat <<EOF > $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040095*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
96EOF
Dean Troyer3ef23bc2014-07-25 14:56:22 -050097 chmod 0600 $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040098 else
Dean Troyer3ef23bc2014-07-25 14:56:22 -050099 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -0400100 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100101 if is_ubuntu; then
102 install_package postgresql
103 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400104 install_package postgresql-server
105 else
Vincent Untz00011c02012-12-06 09:56:32 +0100106 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -0400107 fi
108}
109
110function database_connection_url_postgresql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100111 local db=$1
112 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400113}
114
Dean Troyercc6b4432013-04-08 15:38:03 -0500115
Terry Wilson428af5a2012-11-01 16:12:39 -0400116# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600117$PG_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400118
119# Local variables:
120# mode: shell-script
121# End: