blob: 204c257a676e7d27243499828f58a4ce6f5aa83d [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
Ian Wienand523f4882015-10-13 11:03:03 +110011_XTRACE_PG=$(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
Julien Danjou0eec4f82015-09-08 10:45:06 +000024function get_database_type_postgresql {
25 echo postgresql
26}
27
Dean Troyer995eb922013-03-07 16:11:40 -060028# Get rid of everything enough to cleanly change database backends
29function cleanup_database_postgresql {
30 stop_service postgresql
31 if is_ubuntu; then
32 # Get ruthless with mysql
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010033 apt_get purge -y postgresql*
Dean Troyer995eb922013-03-07 16:11:40 -060034 return
Thomas Bechtolda6509012014-06-23 13:47:36 +020035 elif is_fedora || is_suse; then
Dean Troyer995eb922013-03-07 16:11:40 -060036 uninstall_package postgresql-server
37 else
38 return
39 fi
40}
41
Terry Wilson428af5a2012-11-01 16:12:39 -040042function recreate_database_postgresql {
43 local db=$1
Terry Wilson428af5a2012-11-01 16:12:39 -040044 # Avoid unsightly error when calling dropdb when the database doesn't exist
45 psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
Ihar Hrachyshka157c84b2014-10-06 13:29:39 +020046 createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E utf8 $db
Terry Wilson428af5a2012-11-01 16:12:39 -040047}
48
49function configure_database_postgresql {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050050 local pg_conf pg_dir pg_hba root_roles
Terry Wilson428af5a2012-11-01 16:12:39 -040051 echo_summary "Configuring and starting PostgreSQL"
Vincent Untzb1b04d02012-12-06 11:59:29 +010052 if is_fedora; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050053 pg_hba=/var/lib/pgsql/data/pg_hba.conf
54 pg_conf=/var/lib/pgsql/data/postgresql.conf
55 if ! sudo [ -e $pg_hba ]; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010056 sudo postgresql-setup initdb
Attila Fazekas315f7b02014-01-27 09:40:29 +010057 fi
Vincent Untzb1b04d02012-12-06 11:59:29 +010058 elif is_ubuntu; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050059 pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
60 pg_hba=$pg_dir/pg_hba.conf
61 pg_conf=$pg_dir/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010062 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050063 pg_hba=/var/lib/pgsql/data/pg_hba.conf
64 pg_conf=/var/lib/pgsql/data/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010065 # initdb is called when postgresql is first started
Dean Troyer3ef23bc2014-07-25 14:56:22 -050066 sudo [ -e $pg_hba ] || start_service postgresql
Vincent Untzb1b04d02012-12-06 11:59:29 +010067 else
68 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040069 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040070 # Listen on all addresses
Dean Troyer3ef23bc2014-07-25 14:56:22 -050071 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf
Matt Riedemann94c654e2014-07-09 12:38:36 -070072 # Set max_connections
Dean Troyer3ef23bc2014-07-25 14:56:22 -050073 sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf
Terry Wilson428af5a2012-11-01 16:12:39 -040074 # Do password auth from all IPv4 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050075 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 -040076 # Do password auth for all IPv6 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050077 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 +010078 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040079
Chmouel Boudjnah00b43412014-01-02 10:33:21 +000080 # Create the role if it's not here or else alter it.
81 root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
82 if [[ ${root_roles} == *HERE ]];then
83 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
84 else
85 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
86 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040087}
88
89function install_database_postgresql {
90 echo_summary "Installing postgresql"
Dean Troyer3ef23bc2014-07-25 14:56:22 -050091 local pgpass=$HOME/.pgpass
92 if [[ ! -e $pgpass ]]; then
93 cat <<EOF > $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040094*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
95EOF
Dean Troyer3ef23bc2014-07-25 14:56:22 -050096 chmod 0600 $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040097 else
Dean Troyer3ef23bc2014-07-25 14:56:22 -050098 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -040099 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100100 if is_ubuntu; then
101 install_package postgresql
102 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400103 install_package postgresql-server
Zhang Jinnan4d8c03a2015-08-20 10:00:20 -0400104 if is_fedora; then
105 sudo systemctl enable postgresql-server
106 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400107 else
Vincent Untz00011c02012-12-06 09:56:32 +0100108 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -0400109 fi
Dean Troyer5686dbc2015-03-09 14:27:51 -0500110}
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600111
Dean Troyer5686dbc2015-03-09 14:27:51 -0500112function install_database_python_postgresql {
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600113 # Install Python client module
Sean Dague60996b12015-04-08 09:06:49 -0400114 pip_install_gr psycopg2
Dean Troyer5686dbc2015-03-09 14:27:51 -0500115 ADDITIONAL_VENV_PACKAGES+=",psycopg2"
Terry Wilson428af5a2012-11-01 16:12:39 -0400116}
117
118function database_connection_url_postgresql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100119 local db=$1
120 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400121}
122
Dean Troyercc6b4432013-04-08 15:38:03 -0500123
Terry Wilson428af5a2012-11-01 16:12:39 -0400124# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100125$_XTRACE_PG
Sean Dague584d90e2013-03-29 14:34:53 -0400126
127# Local variables:
128# mode: shell-script
129# End: