Skip to content

Instantly share code, notes, and snippets.

@matthewjackowski
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjackowski/392f23d5b584429ff66e to your computer and use it in GitHub Desktop.
Save matthewjackowski/392f23d5b584429ff66e to your computer and use it in GitHub Desktop.
Postgresql on a Vagrant

For this Gist we'll be using Chef Kitchen: https://docs.getchef.com/kitchen.html

Which is a convient way to wrap together chef-solo, berkshelf, and vagrant.

Assuming a basic setup of Ruby to run Kitchen(I use rbenv and bundler to make life easier, but this might lead to some righteous Ruby debate...so foregoing here).

Enough preamble, here is my Gemfile:

source 'https://rubygems.org'

gem 'berkshelf'

# Uncomment these lines if you want to live on the Edge:
#
# group :development do
#   gem "berkshelf", github: "berkshelf/berkshelf"
#   gem "vagrant", github: "mitchellh/vagrant", tag: "v1.5.2"
# end
#
# group :plugins do
#   gem "vagrant-berkshelf", github: "berkshelf/vagrant-berkshelf"
#   gem "vagrant-omnibus", github: "schisamo/vagrant-omnibus"
# end

gem 'test-kitchen'
gem 'kitchen-vagrant'
gem 'guard'
gem 'guard-kitchen'

With all of the Ruby deps installed. Next we need to go grab some Chef cookbooks with berks. Here is my Berksfile:

source "https://api.berkshelf.com"

cookbook "apt"
cookbook "dmg", "<= 2.0.6"
cookbook "git"
cookbook "curl"
cookbook "rvm", git: 'https://github.com/fnichol/chef-rvm.git'
cookbook "postgresql", git: 'https://github.com/phlipper/chef-postgresql.git'

And now a brief chat about Ruby on vms. I generally include fnichol's excellent rvm cookbook because it's useful to be able to run Rubies that are NOT Chef Ruby.
Also we are using philpper's cookbook for an apt-get install of postgres...which should work fine with targeted Ubuntu 12.04. If you have a burning desire to use other distros and build your own postgres...you'll need a different cookbook.

Finally last config-file, the kitchen.yml is here:

---
driver:
  name: vagrant

provisioner:
  name: chef_solo

platforms:
  - name: ubuntu-12.04
    driver:
      box: ubchef11
      box_url: https://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-i386-vagrant-disk1.box

suites:
  - name: default
    driver_config:
      network:
      - ['forwarded_port', {guest: '5432', host: '5432'}]
      - ['private_network', {ip: '192.168.231.10'}]
    run_list:
      - recipe[apt]
      - recipe[git]
      - recipe[rvm]
      - recipe[postgresql]
      - recipe[postgresql::client]
      - recipe[postgresql::server]
      - recipe[postgresql::contrib]
      - recipe[postgresql::configuration]
      - recipe[postgresql::pg_user]
      - recipe[postgresql::data_directory]
      - recipe[postgresql::pg_database]
      - recipe[postgresql::service]
    attributes: {
                postgresql: {
                  listen_addresses : "*",
                  pg_hba : [{type : "host",db : "all",user : "all",addr : "0.0.0.0/0",method : "trust"}]
                }
              }

With all that in place, all you should need todo is run:

$kitchen converge

The server should build and postgres should auto start if everything goes well. The settings allow for a very permission local network connection (the vm will not be accessible from the world outside your local env).

Here are the default settings you can use to connect via a client like pgamin 3:

host=192.168.231.10
port=5432
user=postgres
password=
db=postgres

JDBC connection url strings would look like this:

db.url="postgres://postgres:postgres@192.168.231.10:5432/postgres?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory"

####Creds Excellent cookbooks from:

Other projects referenced

:octocat:fin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment