Skip to content

Instantly share code, notes, and snippets.

@twolfson
Created November 11, 2015 03:10
Show Gist options
  • Save twolfson/9cf0ae454be269f45af8 to your computer and use it in GitHub Desktop.
Save twolfson/9cf0ae454be269f45af8 to your computer and use it in GitHub Desktop.
Configure PostgreSQL for development in Vagrant

While trying to get a local version of issue_stats running, we ran into a problem we previously experienced with respect to using an MD5 password on a Vagrant box.

Since these commands aren't the most trivial, we figure it would be a good idea to document.

Here's how to add a new user to PostgreSQL in Vagrant:

# Set up PostgreSQL 9.3 configuration
# Modified from https://github.com/twolfson/vagrant-nodebugme/blob/1.0.0/bin/bootstrap.sh#L26-L54
# If we can't open `psql` as `vagrant`
echo_command="psql --db postgres --command \"SELECT 'hai';\""
if ! sudo su vagrant --command "$echo_command" &> /dev/null; then
  # Set up `vagrant` user in PostgreSQL
  create_user_command="psql --command \"CREATE ROLE vagrant WITH SUPERUSER CREATEDB LOGIN;\""
  sudo su postgres --command "$create_user_command"
  set_user_password="psql --command \"ALTER ROLE vagrant WITH PASSWORD 'vagrant';\""
  sudo su postgres --command "$set_user_password"
fi

# If you want to lock down users for security, edit the following
# `local all all   peer` to per-user in `/etc/postgresql/9.3/main/pg_hba.conf`
# e.g. `local all vagrant    peer`

# If you want external access (e.g Vagrant to host machine), edit the following
# Add `listen_addresses = '*'` in `/etc/postgresql/9.3/main/postgresql.conf`
# Add host IP to `pg_hba.conf`
#   host_ip="$(netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10)"
#   echo "host    all             all             $host_ip/0              md5" >> /etc/postgresql/9.3/main/pg_hba.conf

# If you made any edits via the commented lines, then restart the server
# sudo /etc/init.d/postgresql restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment