Installing the Postgres gem on OSX using Postgres.app

Here is a quick tip on how to install the pg ruby gem on OSX if you only have Postgres.app installed.

First, if you attempt to install the pg gem it will fail with:

$ gem install pg
Fetching: pg-0.17.1.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
  ERROR: Failed to build gem native extension.

    /Users/me/.rbenv/versions/2.1.5/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

If you search for solutions to this issue you will undoubtedly be told to install the postgresql package from Homebrew. That's nice, but you already have the Postgres.app and you don't want to maintain 2 versions of the same application on your machine (it is also not isolated, and can cause port conflicts).

What you need to do is tell Rubygems where to find your version of the pg_config file, since it isn't in the default directory.

$ gem install pg -- --with-pg-config=/Application/Postgres.app/Contents/Versions/9.3/bin/pg_config
Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config'
This could take a while...
Successfully installed pg-0.17.1
1 gem installed

The installation will succeed and you can begin using the gem in your projects.