OSX Ruby Rails Postgresql Quickstart

Posted by Tejus Parikh on February 1, 2007

My goal is pretty simple. I want to use my favorite database PostgreSQL, with my favorite environment (Ruby on Rails), on my favorite OS (Mac OSX). Unfortunately, this wasn’t as straight-forward as I could have hoped. After shifting through various conflicting and inconclusive sources of information including the occasionally cryptic Ruby Wiki and the Postgresql docs, I decided that I’d try to condense this information down for those of us that just want to write some code. I found the easiest and most trouble free way to do this is to install Mac Ports. Their instructions are good and make sense, so there is no need to elaborate here. Just make sure you have Xcode installed. With that out of the way, to install Postgresql type:

sudo port install postgresql82 postgresql82-server

Along with installing postgres, it’ll put up a bunch of instructions about how to install postgres as a service, and create a new database. Unlike normal unix installs, the default postgres user is postgres82. The next step is to update Ruby, since the Ruby that is shipped with Tiger is broken. The simplest way is to use ports again:

sudo port install ruby rb-rubygems

You might need to add /opt/local/bin to your PATH environment variable in order to use your updated Ruby instead of the system one. Next, install Rails with:

sudo gem install rails -y

Now for the final and most complicated step, installing a Postgres ruby driver. There are two available drivers, a native one and a pure ruby implementation. For performance reasons, the native driver is preferred, however it is a little unintuitive to install. You first need to set some environment variables (these examples assume the bash shell): If you miss that step, you’ll receive numerous errors from gcc. The final step is (as root):

POSTGRES_INCLUDE=/opt/local/include/postgresql82 gem install postgres -- --with-pgsql-lib-dir=/opt/local/lib/postgresql82

At this point, you should be ready to start developing your Rails apps against Postgres.

Related Posts:

Tejus Parikh

Tejus is an software developer, now working at large companies. Find out when I write new posts on twitter, via RSS or subscribe to the newsletter: