Sean Dague | e263c82 | 2014-12-05 14:25:28 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
Dean Troyer | 6d04fd7 | 2012-12-21 11:03:37 -0600 | [diff] [blame] | 3 | # lib/databases/postgresql |
| 4 | # Functions to control the configuration and operation of the **PostgreSQL** database backend |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 5 | |
| 6 | # Dependencies: |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 7 | # |
| 8 | # - DATABASE_{HOST,USER,PASSWORD} must be defined |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 9 | |
| 10 | # Save trace setting |
Dean Troyer | 41bf452 | 2013-01-28 14:04:39 -0600 | [diff] [blame] | 11 | PG_XTRACE=$(set +o | grep xtrace) |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 12 | set +o xtrace |
| 13 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 14 | |
Matt Riedemann | 94c654e | 2014-07-09 12:38:36 -0700 | [diff] [blame] | 15 | MAX_DB_CONNECTIONS=${MAX_DB_CONNECTIONS:-200} |
| 16 | |
| 17 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 18 | register_database postgresql |
| 19 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 20 | |
| 21 | # Functions |
| 22 | # --------- |
| 23 | |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 24 | # Get rid of everything enough to cleanly change database backends |
| 25 | function cleanup_database_postgresql { |
| 26 | stop_service postgresql |
| 27 | if is_ubuntu; then |
| 28 | # Get ruthless with mysql |
Sahid Orentino Ferdjaoui | e964827 | 2014-02-23 18:55:51 +0100 | [diff] [blame] | 29 | apt_get purge -y postgresql* |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 30 | return |
Thomas Bechtold | a650901 | 2014-06-23 13:47:36 +0200 | [diff] [blame] | 31 | elif is_fedora || is_suse; then |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 32 | uninstall_package postgresql-server |
| 33 | else |
| 34 | return |
| 35 | fi |
| 36 | } |
| 37 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 38 | function recreate_database_postgresql { |
| 39 | local db=$1 |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 40 | # Avoid unsightly error when calling dropdb when the database doesn't exist |
| 41 | psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db" |
Ihar Hrachyshka | 157c84b | 2014-10-06 13:29:39 +0200 | [diff] [blame] | 42 | createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E utf8 $db |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | function configure_database_postgresql { |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 46 | local pg_conf pg_dir pg_hba root_roles |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 47 | echo_summary "Configuring and starting PostgreSQL" |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 48 | if is_fedora; then |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 49 | pg_hba=/var/lib/pgsql/data/pg_hba.conf |
| 50 | pg_conf=/var/lib/pgsql/data/postgresql.conf |
| 51 | if ! sudo [ -e $pg_hba ]; then |
Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame^] | 52 | sudo postgresql-setup initdb |
Attila Fazekas | 315f7b0 | 2014-01-27 09:40:29 +0100 | [diff] [blame] | 53 | fi |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 54 | elif is_ubuntu; then |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 55 | pg_dir=`find /etc/postgresql -name pg_hba.conf|xargs dirname` |
| 56 | pg_hba=$pg_dir/pg_hba.conf |
| 57 | pg_conf=$pg_dir/postgresql.conf |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 58 | elif is_suse; then |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 59 | pg_hba=/var/lib/pgsql/data/pg_hba.conf |
| 60 | pg_conf=/var/lib/pgsql/data/postgresql.conf |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 61 | # initdb is called when postgresql is first started |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 62 | sudo [ -e $pg_hba ] || start_service postgresql |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 63 | else |
| 64 | exit_distro_not_supported "postgresql configuration" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 65 | fi |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 66 | # Listen on all addresses |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 67 | sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $pg_conf |
Matt Riedemann | 94c654e | 2014-07-09 12:38:36 -0700 | [diff] [blame] | 68 | # Set max_connections |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 69 | sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $pg_conf |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 70 | # Do password auth from all IPv4 clients |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 71 | 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 Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 72 | # Do password auth for all IPv6 clients |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 73 | sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $pg_hba |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 74 | restart_service postgresql |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 75 | |
Chmouel Boudjnah | 00b4341 | 2014-01-02 10:33:21 +0000 | [diff] [blame] | 76 | # Create the role if it's not here or else alter it. |
| 77 | root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'") |
| 78 | if [[ ${root_roles} == *HERE ]];then |
| 79 | sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" |
| 80 | else |
| 81 | sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" |
| 82 | fi |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | function install_database_postgresql { |
| 86 | echo_summary "Installing postgresql" |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 87 | local pgpass=$HOME/.pgpass |
| 88 | if [[ ! -e $pgpass ]]; then |
| 89 | cat <<EOF > $pgpass |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 90 | *:*:*:$DATABASE_USER:$DATABASE_PASSWORD |
| 91 | EOF |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 92 | chmod 0600 $pgpass |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 93 | else |
Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 94 | sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $pgpass |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 95 | fi |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 96 | if is_ubuntu; then |
| 97 | install_package postgresql |
| 98 | elif is_fedora || is_suse; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 99 | install_package postgresql-server |
| 100 | else |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 101 | exit_distro_not_supported "postgresql installation" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 102 | fi |
| 103 | } |
| 104 | |
| 105 | function database_connection_url_postgresql { |
Attila Fazekas | 7e79d91 | 2013-03-03 12:23:04 +0100 | [diff] [blame] | 106 | local db=$1 |
| 107 | echo "$BASE_SQL_CONN/$db?client_encoding=utf8" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 110 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 111 | # Restore xtrace |
Dean Troyer | 41bf452 | 2013-01-28 14:04:39 -0600 | [diff] [blame] | 112 | $PG_XTRACE |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 113 | |
| 114 | # Local variables: |
| 115 | # mode: shell-script |
| 116 | # End: |