blob: 519479ad68c9ab50f9b78708c09fcd6d0d6c6f8e [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
Terry Wilson428af5a2012-11-01 16:12:39 -040013register_database postgresql
14
Dean Troyercc6b4432013-04-08 15:38:03 -050015
16# Functions
17# ---------
18
Dean Troyer995eb922013-03-07 16:11:40 -060019# Get rid of everything enough to cleanly change database backends
20function cleanup_database_postgresql {
21 stop_service postgresql
22 if is_ubuntu; then
23 # Get ruthless with mysql
24 sudo aptitude purge -y ~npostgresql
25 return
26 elif is_fedora; then
27 uninstall_package postgresql-server
28 else
29 return
30 fi
31}
32
Terry Wilson428af5a2012-11-01 16:12:39 -040033function recreate_database_postgresql {
34 local db=$1
35 local charset=$2
36 # Avoid unsightly error when calling dropdb when the database doesn't exist
37 psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
38 createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E $charset $db
39}
40
41function configure_database_postgresql {
42 echo_summary "Configuring and starting PostgreSQL"
Vincent Untzb1b04d02012-12-06 11:59:29 +010043 if is_fedora; then
Terry Wilson428af5a2012-11-01 16:12:39 -040044 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
45 PG_CONF=/var/lib/pgsql/data/postgresql.conf
Terry Wilsonadfc7a32012-11-20 13:08:13 -050046 sudo [ -e $PG_HBA ] || sudo postgresql-setup initdb
Vincent Untzb1b04d02012-12-06 11:59:29 +010047 elif is_ubuntu; then
Terry Wilson428af5a2012-11-01 16:12:39 -040048 PG_DIR=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
49 PG_HBA=$PG_DIR/pg_hba.conf
50 PG_CONF=$PG_DIR/postgresql.conf
Vincent Untzb1b04d02012-12-06 11:59:29 +010051 elif is_suse; then
52 PG_HBA=/var/lib/pgsql/data/pg_hba.conf
53 PG_CONF=/var/lib/pgsql/data/postgresql.conf
54 # initdb is called when postgresql is first started
55 sudo [ -e $PG_HBA ] || start_service postgresql
56 else
57 exit_distro_not_supported "postgresql configuration"
Terry Wilson428af5a2012-11-01 16:12:39 -040058 fi
Terry Wilson428af5a2012-11-01 16:12:39 -040059 # Listen on all addresses
60 sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $PG_CONF
61 # Do password auth from all IPv4 clients
62 sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $PG_HBA
63 # Do password auth for all IPv6 clients
64 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 +010065 restart_service postgresql
Terry Wilson428af5a2012-11-01 16:12:39 -040066
67 # If creating the role fails, chances are it already existed. Try to alter it.
Dean Troyerc1b486a2012-11-05 14:26:09 -060068 sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" || \
69 sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
Terry Wilson428af5a2012-11-01 16:12:39 -040070}
71
72function install_database_postgresql {
73 echo_summary "Installing postgresql"
74 PGPASS=$HOME/.pgpass
75 if [[ ! -e $PGPASS ]]; then
76 cat <<EOF > $PGPASS
77*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
78EOF
79 chmod 0600 $PGPASS
80 else
81 sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $PGPASS
82 fi
Vincent Untz00011c02012-12-06 09:56:32 +010083 if is_ubuntu; then
84 install_package postgresql
85 elif is_fedora || is_suse; then
Terry Wilson428af5a2012-11-01 16:12:39 -040086 install_package postgresql-server
87 else
Vincent Untz00011c02012-12-06 09:56:32 +010088 exit_distro_not_supported "postgresql installation"
Terry Wilson428af5a2012-11-01 16:12:39 -040089 fi
90}
91
92function database_connection_url_postgresql {
Attila Fazekas7e79d912013-03-03 12:23:04 +010093 local db=$1
94 echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
Terry Wilson428af5a2012-11-01 16:12:39 -040095}
96
Dean Troyercc6b4432013-04-08 15:38:03 -050097
Terry Wilson428af5a2012-11-01 16:12:39 -040098# Restore xtrace
Dean Troyer41bf4522013-01-28 14:04:39 -060099$PG_XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400100
101# Local variables:
102# mode: shell-script
103# End: