Installing the pg PostgreSQL gem on Mac OS X

I've recently switched from using MySQL to PostgreSQL for my Ruby projects. You can connect to a PostgreSQL database from Ruby using the pg gem, but if the pg_config program isn't in your path you'll run into problems during installation.

$ sudo gem install pg 
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install pg
extconf.rb:1: command not found: pg_config --version
ERROR: can't find pg_config.
HINT: Make sure pg_config is in your PATH

A few minutes later I realised I'd installed my copy of PostgreSQL using the one click installer, which puts the database server in the /Library/PostgreSQL directory. This sorted it:

$ PATH=$PATH:/Library/PostgreSQL/8.3/bin sudo gem install pg

I love feedback and questions — please get in touch on Twitter or leave a comment.