|
| 1 | +#!/bin/sh -e |
| 2 | + |
| 3 | +# Edit the following to change the name of the database user that will be created: |
| 4 | +APP_DB_USER=submit |
| 5 | +APP_DB_PASS=password |
| 6 | + |
| 7 | +# Edit the following to change the name of the database that is created (defaults to the user name) |
| 8 | +APP_DB_NAME=submit |
| 9 | + |
| 10 | +# Edit the following to change the version of PostgreSQL that is installed |
| 11 | +PG_VERSION=9.6 |
| 12 | + |
| 13 | +########################################################### |
| 14 | +# Changes below this line are probably not necessary |
| 15 | +########################################################### |
| 16 | +print_db_usage () { |
| 17 | + echo "Your PostgreSQL database has been setup and can be accessed on your local machine on the forwarded port (default: 15432)" |
| 18 | + echo " Host: localhost" |
| 19 | + echo " Port: 15432" |
| 20 | + echo " Database: $APP_DB_NAME" |
| 21 | + echo " Username: $APP_DB_USER" |
| 22 | + echo " Password: $APP_DB_PASS" |
| 23 | + echo "" |
| 24 | + echo "Admin access to postgres user via VM:" |
| 25 | + echo " vagrant ssh" |
| 26 | + echo " sudo su - postgres" |
| 27 | + echo "" |
| 28 | + echo "psql access to app database user via VM:" |
| 29 | + echo " vagrant ssh" |
| 30 | + echo " sudo su - postgres" |
| 31 | + echo " PGUSER=$APP_DB_USER PGPASSWORD=$APP_DB_PASS psql -h localhost $APP_DB_NAME" |
| 32 | + echo "" |
| 33 | + echo "Env variable for application development:" |
| 34 | + echo " DATABASE_URL=postgresql://$APP_DB_USER:$APP_DB_PASS@localhost:15432/$APP_DB_NAME" |
| 35 | + echo "" |
| 36 | + echo "Local command to access the database via psql:" |
| 37 | + echo " PGUSER=$APP_DB_USER PGPASSWORD=$APP_DB_PASS psql -h localhost -p 15432 $APP_DB_NAME" |
| 38 | +} |
| 39 | + |
| 40 | +export DEBIAN_FRONTEND=noninteractive |
| 41 | + |
| 42 | +PROVISIONED_ON=/etc/vm_provision_on_timestamp |
| 43 | +if [ -f "$PROVISIONED_ON" ] |
| 44 | +then |
| 45 | + echo "VM was already provisioned at: $(cat $PROVISIONED_ON)" |
| 46 | + echo "To run system updates manually login via 'vagrant ssh' and run 'apt-get update && apt-get upgrade'" |
| 47 | + echo "" |
| 48 | + print_db_usage |
| 49 | + exit |
| 50 | +fi |
| 51 | + |
| 52 | +PG_REPO_APT_SOURCE=/etc/apt/sources.list.d/pgdg.list |
| 53 | +if [ ! -f "$PG_REPO_APT_SOURCE" ] |
| 54 | +then |
| 55 | + # Add PG apt repo: |
| 56 | + echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" > "$PG_REPO_APT_SOURCE" |
| 57 | + |
| 58 | + # Add PGDG repo key: |
| 59 | + wget --quiet -O - https://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | apt-key add - |
| 60 | +fi |
| 61 | + |
| 62 | +# Update package list and upgrade all packages |
| 63 | +apt-get update |
| 64 | +apt-get -y upgrade |
| 65 | + |
| 66 | +apt-get -y install "postgresql-$PG_VERSION" "postgresql-contrib-$PG_VERSION" |
| 67 | + |
| 68 | +PG_CONF="/etc/postgresql/$PG_VERSION/main/postgresql.conf" |
| 69 | +PG_HBA="/etc/postgresql/$PG_VERSION/main/pg_hba.conf" |
| 70 | +PG_DIR="/var/lib/postgresql/$PG_VERSION/main" |
| 71 | + |
| 72 | +# Edit postgresql.conf to change listen address to '*': |
| 73 | +sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" "$PG_CONF" |
| 74 | + |
| 75 | +# Append to pg_hba.conf to add password auth: |
| 76 | +echo "host all all all md5" >> "$PG_HBA" |
| 77 | + |
| 78 | +# Explicitly set default client_encoding |
| 79 | +echo "client_encoding = utf8" >> "$PG_CONF" |
| 80 | + |
| 81 | +# Restart so that all new config is loaded: |
| 82 | +service postgresql restart |
| 83 | + |
| 84 | +cat << EOF | su - postgres -c psql |
| 85 | +-- Create the database user: |
| 86 | +CREATE USER $APP_DB_USER WITH PASSWORD '$APP_DB_PASS'; |
| 87 | +
|
| 88 | +-- Create the database: |
| 89 | +CREATE DATABASE $APP_DB_NAME WITH OWNER=$APP_DB_USER |
| 90 | + LC_COLLATE='en_US.utf8' |
| 91 | + LC_CTYPE='en_US.utf8' |
| 92 | + ENCODING='UTF8' |
| 93 | + TEMPLATE=template0; |
| 94 | +EOF |
| 95 | + |
| 96 | +# Tag the provision time: |
| 97 | +date > "$PROVISIONED_ON" |
| 98 | + |
| 99 | +echo "Successfully created PostgreSQL dev virtual machine." |
| 100 | +echo "" |
| 101 | +print_db_usage |
0 commit comments