blob: 14425a53b7ce5a89f03a067b74ef2b1a119dcf9f [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 {
Yalei Wang174986d2016-03-02 03:28:06 +000050 local pg_conf pg_dir pg_hba root_roles version
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
Yalei Wang174986d2016-03-02 03:28:06 +000059 version=`psql --version | cut -d ' ' -f3 | cut -d. -f1-2`
60 if vercmp $version '>=' 9.3; then
61 if [ -z "`pg_lsclusters -h`" ]; then
62 echo 'No PostgreSQL clusters exist; will create one'
63 sudo pg_createcluster $version main --start
64 fi
65 fi
Dean Troyer3ef23bc2014-07-25 14:56:22 -050066 pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
67 pg_hba=$pg_dir/pg_hba.conf
68 pg_conf=$pg_dir/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010069 elif is_suse; then
Dean Troyer3ef23bc2014-07-25 14:56:22 -050070 pg_hba=/var/lib/pgsql/data/pg_hba.conf
71 pg_conf=/var/lib/pgsql/data/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010072 # initdb is called when postgresql is first started
Dean Troyer3ef23bc2014-07-25 14:56:22 -050073 sudo [ -e $pg_hba ] || start_service postgresql
Vincent Untzb1b04d02012-12-06 11:59:29 +010074 else
75 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040076 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040077 # Listen on all addresses
Dean Troyer3ef23bc2014-07-25 14:56:22 -050078 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf
Matt Riedemann94c654e2014-07-09 12:38:36 -070079 # Set max_connections
Dean Troyer3ef23bc2014-07-25 14:56:22 -050080 sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf
Terry Wilson428af5a2012-11-01 16:12:39 -040081 # Do password auth from all IPv4 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050082 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 -040083 # Do password auth for all IPv6 clients
Dean Troyer3ef23bc2014-07-25 14:56:22 -050084 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 +010085 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040086
Chmouel Boudjnah00b43412014-01-02 10:33:21 +000087 # Create the role if it's not here or else alter it.
88 root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
89 if [[ ${root_roles} == *HERE ]];then
90 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
91 else
92 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
93 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040094}
95
96function install_database_postgresql {
97 echo_summary "Installing postgresql"
Dean Troyer3ef23bc2014-07-25 14:56:22 -050098 local pgpass=$HOME/.pgpass
99 if [[ ! -e $pgpass ]]; then
100 cat <<EOF > $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -0400101*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
102EOF
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500103 chmod 0600 $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -0400104 else
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500105 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass
Terry Wilson428af5a2012-11-01 16:12:39 -0400106 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100107 if is_ubuntu; then
108 install_package postgresql
109 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400110 install_package postgresql-server
Zhang Jinnan4d8c03a2015-08-20 10:00:20 -0400111 if is_fedora; then
gordon chungb9201cc2016-02-18 15:50:01 -0500112 sudo systemctl enable postgresql
Zhang Jinnan4d8c03a2015-08-20 10:00:20 -0400113 fi
Terry Wilson428af5a2012-11-01 16:12:39 -0400114 else
Vincent Untz00011c02012-12-06 09:56:32 +0100115 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -0400116 fi
Dean Troyer5686dbc2015-03-09 14:27:51 -0500117}
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600118
Dean Troyer5686dbc2015-03-09 14:27:51 -0500119function install_database_python_postgresql {
Dean Troyerb1d8e8e2015-02-16 13:58:35 -0600120 # Install Python client module
Sean Dague60996b12015-04-08 09:06:49 -0400121 pip_install_gr psycopg2
Dean Troyer5686dbc2015-03-09 14:27:51 -0500122 ADDITIONAL_VENV_PACKAGES+=",psycopg2"
Terry Wilson428af5a2012-11-01 16:12:39 -0400123}
124
125function database_connection_url_postgresql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100126 local db=$1
127 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400128}
129
Dean Troyercc6b4432013-04-08 15:38:03 -0500130
Terry Wilson428af5a2012-11-01 16:12:39 -0400131# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100132$_XTRACE_PG
Sean Dague584d90e2013-03-29 14:34:53 -0400133
134# Local variables:
135# mode: shell-script
136# End: