haiku code

Installing PostgreSQL via Vagrant for Rails Development

January 13, 2014

As I’ve mentioned a couple months back in a discussion on Twitter, I think PostgreSQL would be much more popular among Rails programmers if not for the tiring setup. So let’s simplify it a bit with Vagrant.

Last time we’ve created a simple Vagrantfile that allowed us to start a bare Ubuntu box to use for our Rails development. Now we’ll change it a bit in order to use PostgreSQL instead of MySQL. Here it is:

# Berksfile
site :opscode

cookbook 'apt'
cookbook 'database'
cookbook 'set_locale', path: './cookbooks/set_locale'
cookbook 'postgresql_server_utf8', path: './cookbooks/postgresql_server_utf8'


# Vagrantfile

require 'berkshelf/vagrant'

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "precise32"

  # Use Berksfile
  config.berkshelf.enabled = true

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  config.vm.provision :chef_solo do |chef|
    chef.add_recipe 'apt'
    chef.add_recipe 'locale'
    chef.add_recipe 'postgresql_server_utf8'

    chef.json = {
      'postgresql' => {
        'version' => '9.1',
        'initdb_locale' => 'en_US.UTF-8',
        'password' => {'postgres' => 'mysuperfancypassword'},
        'config' => {
          'lc_messages' => 'en_US.UTF-8',
          'lc_monetary' => 'en_US.UTF-8',
          'lc_numeric' => 'en_US.UTF-8',
          'lc_time' => 'en_US.UTF-8'
        }
      }
    }
  end
end

In order for this to work, we need to import two recipes to a cookbooks directory in our app: set_locale and postgresqlserverutf8. They take care of setting the proper encoding to get a UTF8 locale database.

And that’s all, folks.


Written by Wojciech Ogrodowczyk who takes photos, climbs mountains, and runs Brains & Beards to help companies deliver better mobile applications faster.

© 2012 - 2024, built in a 🚐 with Gatsby and ☀️