blob: 2aa38ccf76d609f72fe443650642b5bfdefedae0 [file] [log] [blame]
Matt Riedemannb14665f2019-10-17 19:34:05 +00001#!/bin/bash
2#
3# lib/databases/postgresql
4# Functions to control the configuration and operation of the **PostgreSQL** database backend
5
6# Dependencies:
7#
8# - DATABASE_{HOST,USER,PASSWORD} must be defined
9
10# Save trace setting
11_XTRACE_PG=$(set +o | grep xtrace)
12set +o xtrace
13
14
15MAX_DB_CONNECTIONS=${MAX_DB_CONNECTIONS:-200}
Carlos Camachocc6e20b2022-01-07 15:30:56 +010016INSTALL_DATABASE_SERVER_PACKAGES=$(trueorfalse True INSTALL_DATABASE_SERVER_PACKAGES)
Matt Riedemannb14665f2019-10-17 19:34:05 +000017
18register_database postgresql
19
20
21# Functions
22# ---------
23
24function get_database_type_postgresql {
25 echo postgresql
26}
27
28# 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
33 apt_get purge -y postgresql*
34 return
Martin Kopecec07b342023-01-24 17:38:45 +010035 elif is_fedora; then
Matt Riedemannb14665f2019-10-17 19:34:05 +000036 uninstall_package postgresql-server
37 else
38 return
39 fi
40}
41
42function recreate_database_postgresql {
43 local db=$1
44 # 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"
46 createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E utf8 $db
47}
48
Rodolfo Alonso Hernandezf49d4752024-10-02 09:36:55 +000049function _exit_pg_init {
50 sudo cat /var/lib/pgsql/initdb_postgresql.log
51}
52
Matt Riedemannb14665f2019-10-17 19:34:05 +000053function configure_database_postgresql {
54 local pg_conf pg_dir pg_hba check_role version
55 echo_summary "Configuring and starting PostgreSQL"
56 if is_fedora; then
57 pg_hba=/var/lib/pgsql/data/pg_hba.conf
58 pg_conf=/var/lib/pgsql/data/postgresql.conf
59 if ! sudo [ -e $pg_hba ]; then
Rodolfo Alonso Hernandezf49d4752024-10-02 09:36:55 +000060 trap _exit_pg_init EXIT
Matt Riedemannb14665f2019-10-17 19:34:05 +000061 sudo postgresql-setup initdb
Rodolfo Alonso Hernandezf49d4752024-10-02 09:36:55 +000062 trap - EXIT
Matt Riedemannb14665f2019-10-17 19:34:05 +000063 fi
64 elif is_ubuntu; then
65 version=`psql --version | cut -d ' ' -f3 | cut -d. -f1-2`
66 if vercmp $version '>=' 9.3; then
67 if [ -z "`pg_lsclusters -h`" ]; then
68 echo 'No PostgreSQL clusters exist; will create one'
69 sudo pg_createcluster $version main --start
70 fi
71 fi
72 pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
73 pg_hba=$pg_dir/pg_hba.conf
74 pg_conf=$pg_dir/postgresql.conf
Matt Riedemannb14665f2019-10-17 19:34:05 +000075 else
76 exit_distro_not_supported "postgresql configuration"
77 fi
78 # Listen on all addresses
79 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf
80 # Set max_connections
81 sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf
82 # Do password auth from all IPv4 clients
83 sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $pg_hba
84 # Do password auth for all IPv6 clients
85 sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $pg_hba
86 restart_service postgresql
87
88 # Create the role if it's not here or else alter it.
89 check_role=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='$DATABASE_USER'")
90 if [[ ${check_role} == *HERE ]];then
91 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
92 else
93 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
94 fi
95}
96
97function install_database_postgresql {
98 echo_summary "Installing postgresql"
Matt Riedemannb14665f2019-10-17 19:34:05 +000099 local pgpass=$HOME/.pgpass
100 if [[ ! -e $pgpass ]]; then
101 cat <<EOF > $pgpass
102*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
103EOF
104 chmod 0600 $pgpass
105 else
106 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass
107 fi
Carlos Camachocc6e20b2022-01-07 15:30:56 +0100108 if [[ "$INSTALL_DATABASE_SERVER_PACKAGES" == "True" ]]; then
109 if is_ubuntu; then
110 install_package postgresql
Martin Kopecec07b342023-01-24 17:38:45 +0100111 elif is_fedora; then
Carlos Camachocc6e20b2022-01-07 15:30:56 +0100112 install_package postgresql-server
113 if is_fedora; then
114 sudo systemctl enable postgresql
115 fi
116 else
117 exit_distro_not_supported "postgresql installation"
Matt Riedemannb14665f2019-10-17 19:34:05 +0000118 fi
Matt Riedemannb14665f2019-10-17 19:34:05 +0000119 fi
120}
121
122function install_database_python_postgresql {
123 # Install Python client module
124 pip_install_gr psycopg2
125 ADDITIONAL_VENV_PACKAGES+=",psycopg2"
126}
127
128function database_connection_url_postgresql {
129 local db=$1
130 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
131}
132
133
134# Restore xtrace
135$_XTRACE_PG
136
137# Local variables:
138# mode: shell-script
139# End: