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