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: |
| 5 | # DATABASE_{HOST,USER,PASSWORD} must be defined |
| 6 | |
| 7 | # Save trace setting |
Dean Troyer | 41bf452 | 2013-01-28 14:04:39 -0600 | [diff] [blame] | 8 | PG_XTRACE=$(set +o | grep xtrace) |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 9 | set +o xtrace |
| 10 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 11 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 12 | register_database postgresql |
| 13 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 14 | |
| 15 | # Functions |
| 16 | # --------- |
| 17 | |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 18 | # Get rid of everything enough to cleanly change database backends |
| 19 | function cleanup_database_postgresql { |
| 20 | stop_service postgresql |
| 21 | if is_ubuntu; then |
| 22 | # Get ruthless with mysql |
| 23 | sudo aptitude purge -y ~npostgresql |
| 24 | return |
| 25 | elif is_fedora; then |
| 26 | uninstall_package postgresql-server |
| 27 | else |
| 28 | return |
| 29 | fi |
| 30 | } |
| 31 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 32 | function recreate_database_postgresql { |
| 33 | local db=$1 |
| 34 | local charset=$2 |
| 35 | # Avoid unsightly error when calling dropdb when the database doesn't exist |
| 36 | psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db" |
| 37 | createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E $charset $db |
| 38 | } |
| 39 | |
| 40 | function configure_database_postgresql { |
| 41 | echo_summary "Configuring and starting PostgreSQL" |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 42 | if is_fedora; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 43 | PG_HBA=/var/lib/pgsql/data/pg_hba.conf |
| 44 | PG_CONF=/var/lib/pgsql/data/postgresql.conf |
Terry Wilson | adfc7a3 | 2012-11-20 13:08:13 -0500 | [diff] [blame] | 45 | sudo [ -e $PG_HBA ] || sudo postgresql-setup initdb |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 46 | elif is_ubuntu; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 47 | PG_DIR=`find /etc/postgresql -name pg_hba.conf|xargs dirname` |
| 48 | PG_HBA=$PG_DIR/pg_hba.conf |
| 49 | PG_CONF=$PG_DIR/postgresql.conf |
Vincent Untz | b1b04d0 | 2012-12-06 11:59:29 +0100 | [diff] [blame] | 50 | elif is_suse; then |
| 51 | PG_HBA=/var/lib/pgsql/data/pg_hba.conf |
| 52 | PG_CONF=/var/lib/pgsql/data/postgresql.conf |
| 53 | # initdb is called when postgresql is first started |
| 54 | sudo [ -e $PG_HBA ] || start_service postgresql |
| 55 | else |
| 56 | exit_distro_not_supported "postgresql configuration" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 57 | fi |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 58 | # Listen on all addresses |
| 59 | sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $PG_CONF |
| 60 | # Do password auth from all IPv4 clients |
| 61 | sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $PG_HBA |
| 62 | # Do password auth for all IPv6 clients |
| 63 | 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] | 64 | restart_service postgresql |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 65 | |
| 66 | # If creating the role fails, chances are it already existed. Try to alter it. |
Dean Troyer | c1b486a | 2012-11-05 14:26:09 -0600 | [diff] [blame] | 67 | sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" || \ |
| 68 | sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | function install_database_postgresql { |
| 72 | echo_summary "Installing postgresql" |
| 73 | PGPASS=$HOME/.pgpass |
| 74 | if [[ ! -e $PGPASS ]]; then |
| 75 | cat <<EOF > $PGPASS |
| 76 | *:*:*:$DATABASE_USER:$DATABASE_PASSWORD |
| 77 | EOF |
| 78 | chmod 0600 $PGPASS |
| 79 | else |
| 80 | sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $PGPASS |
| 81 | fi |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 82 | if is_ubuntu; then |
| 83 | install_package postgresql |
| 84 | elif is_fedora || is_suse; then |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 85 | install_package postgresql-server |
| 86 | else |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 87 | exit_distro_not_supported "postgresql installation" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 88 | fi |
| 89 | } |
| 90 | |
| 91 | function database_connection_url_postgresql { |
Attila Fazekas | 7e79d91 | 2013-03-03 12:23:04 +0100 | [diff] [blame] | 92 | local db=$1 |
| 93 | echo "$BASE_SQL_CONN/$db?client_encoding=utf8" |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 96 | |
Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 97 | # Restore xtrace |
Dean Troyer | 41bf452 | 2013-01-28 14:04:39 -0600 | [diff] [blame] | 98 | $PG_XTRACE |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 99 | |
| 100 | # Local variables: |
| 101 | # mode: shell-script |
| 102 | # End: |