lib/databases/postgresql
e263c82e
 #!/bin/bash
 #
6d04fd7b
 # lib/databases/postgresql
 # Functions to control the configuration and operation of the **PostgreSQL** database backend
428af5a2
 
 # Dependencies:
6a5aa7c6
 #
 # - DATABASE_{HOST,USER,PASSWORD} must be defined
428af5a2
 
 # Save trace setting
41bf4520
 PG_XTRACE=$(set +o | grep xtrace)
428af5a2
 set +o xtrace
 
cc6b4435
 
94c654ef
 MAX_DB_CONNECTIONS=${MAX_DB_CONNECTIONS:-200}
 
 
428af5a2
 register_database postgresql
 
cc6b4435
 
 # Functions
 # ---------
 
0eec4f86
 function get_database_type_postgresql {
     echo postgresql
 }
 
995eb927
 # Get rid of everything enough to cleanly change database backends
 function cleanup_database_postgresql {
     stop_service postgresql
     if is_ubuntu; then
         # Get ruthless with mysql
e9648276
         apt_get purge -y postgresql*
995eb927
         return
a6509018
     elif is_fedora || is_suse; then
995eb927
         uninstall_package postgresql-server
     else
         return
     fi
 }
 
428af5a2
 function recreate_database_postgresql {
     local db=$1
     # Avoid unsightly error when calling dropdb when the database doesn't exist
     psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
157c84b8
     createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E utf8 $db
428af5a2
 }
 
 function configure_database_postgresql {
3ef23bce
     local pg_conf pg_dir pg_hba root_roles
428af5a2
     echo_summary "Configuring and starting PostgreSQL"
b1b04d06
     if is_fedora; then
3ef23bce
         pg_hba=/var/lib/pgsql/data/pg_hba.conf
         pg_conf=/var/lib/pgsql/data/postgresql.conf
         if ! sudo [ -e $pg_hba ]; then
1f316beb
             sudo postgresql-setup initdb
315f7b07
         fi
b1b04d06
     elif is_ubuntu; then
3ef23bce
         pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
         pg_hba=$pg_dir/pg_hba.conf
         pg_conf=$pg_dir/postgresql.conf
b1b04d06
     elif is_suse; then
3ef23bce
         pg_hba=/var/lib/pgsql/data/pg_hba.conf
         pg_conf=/var/lib/pgsql/data/postgresql.conf
b1b04d06
         # initdb is called when postgresql is first started
3ef23bce
         sudo [ -e $pg_hba ] || start_service postgresql
b1b04d06
     else
         exit_distro_not_supported "postgresql configuration"
428af5a2
     fi
     # Listen on all addresses
3ef23bce
     sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf
94c654ef
     # Set max_connections
3ef23bce
     sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf
428af5a2
     # Do password auth from all IPv4 clients
3ef23bce
     sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $pg_hba
428af5a2
     # Do password auth for all IPv6 clients
3ef23bce
     sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $pg_hba
b1b04d06
     restart_service postgresql
428af5a2
 
00b43418
     # Create the role if it's not here or else alter it.
     root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
     if [[ ${root_roles} == *HERE ]];then
         sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
     else
         sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
     fi
428af5a2
 }
 
 function install_database_postgresql {
     echo_summary "Installing postgresql"
3ef23bce
     local pgpass=$HOME/.pgpass
     if [[ ! -e $pgpass ]]; then
         cat <<EOF > $pgpass
428af5a2
 *:*:*:$DATABASE_USER:$DATABASE_PASSWORD
 EOF
3ef23bce
         chmod 0600 $pgpass
428af5a2
     else
3ef23bce
         sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass
428af5a2
     fi
00011c08
     if is_ubuntu; then
         install_package postgresql
     elif is_fedora || is_suse; then
428af5a2
         install_package postgresql-server
4d8c03a3
         if is_fedora; then
             sudo systemctl enable postgresql-server
         fi
428af5a2
     else
00011c08
         exit_distro_not_supported "postgresql installation"
428af5a2
     fi
5686dbc4
 }
b1d8e8e2
 
5686dbc4
 function install_database_python_postgresql {
b1d8e8e2
     # Install Python client module
60996b1b
     pip_install_gr psycopg2
5686dbc4
     ADDITIONAL_VENV_PACKAGES+=",psycopg2"
428af5a2
 }
 
 function database_connection_url_postgresql {
7e79d913
     local db=$1
     echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
428af5a2
 }
 
cc6b4435
 
428af5a2
 # Restore xtrace
41bf4520
 $PG_XTRACE
584d90ec
 
 # Local variables:
 # mode: shell-script
 # End: