blob: 6e85d6ec77b6e9c5e14bde75f7f23a1ee680e6bb [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 {
45 echo_summary "Configuring and starting PostgreSQL"
Vincent Untzb1b04d02012-12-06 11:59:29 +010046 if is_fedora; then
Terry Wilson428af5a2012-11-01 16:12:39 -040047 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
48 PG_CONF=/var/lib/pgsql/data/postgresql.conf
Attila Fazekas315f7b02014-01-27 09:40:29 +010049 if ! sudo [ -e $PG_HBA ]; then
50 if ! [[ $DISTRO =~ (rhel6) ]]; then
51 sudo postgresql-setup initdb
52 else
53 sudo service postgresql initdb
54 fi
55 fi
Vincent Untzb1b04d02012-12-06 11:59:29 +010056 elif is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040057 PG_DIR=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
58 PG_HBA=$PG_DIR/pg_hba.conf
59 PG_CONF=$PG_DIR/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010060 elif is_suse; then
61 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
62 PG_CONF=/var/lib/pgsql/data/postgresql.conf
63 # initdb is called when postgresql is first started
64 sudo [ -e $PG_HBA ] || start_service postgresql
65 else
66 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040067 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040068 # Listen on all addresses
69 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $PG_CONF
Matt Riedemann94c654e2014-07-09 12:38:36 -070070 # Set max_connections
71 sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $PG_CONF
Terry Wilson428af5a2012-11-01 16:12:39 -040072 # Do password auth from all IPv4 clients
73 sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $PG_HBA
74 # Do password auth for all IPv6 clients
75 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 +010076 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040077
Chmouel Boudjnah00b43412014-01-02 10:33:21 +000078 # Create the role if it's not here or else alter it.
79 root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
80 if [[ ${root_roles} == *HERE ]];then
81 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
82 else
83 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
84 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040085}
86
87function install_database_postgresql {
88 echo_summary "Installing postgresql"
89 PGPASS=$HOME/.pgpass
90 if [[ ! -e $PGPASS ]]; then
91 cat <<EOF > $PGPASS
92*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
93EOF
94 chmod 0600 $PGPASS
95 else
96 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $PGPASS
97 fi
Vincent Untz00011c02012-12-06 09:56:32 +010098 if is_ubuntu; then
99 install_package postgresql
100 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -0400101 install_package postgresql-server
102 else
Vincent Untz00011c02012-12-06 09:56:32 +0100103 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -0400104 fi
105}
106
107function database_connection_url_postgresql {
Attila Fazekas7e79d912013-03-03 12:23:04 +0100108 local db=$1
109 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -0400110}
111
Dean Troyercc6b4432013-04-08 15:38:03 -0500112
Terry Wilson428af5a2012-11-01 16:12:39 -0400113# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -0600114$PG_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400115
116# Local variables:
117# mode: shell-script
118# End: