Installing PostgreSQL and pg gem on Mac OSX
Today I was fixing a bug in Rails 3.0.5 (hopefully it gets merged!) and I needed to build some test cases along with my patch. First of all, my original patch was making two db queries when it shouldn’t have been, so it was important that I had test cases to show the bug existed, as well as make sure it was doing the proper amount of querying. Secondly, the empty?
function weren’t any test cases anyways, so it was nice that I was able to provide some basic test coverage for that as well. That’s probably why the issue slipped through several releases (appears to have been around since 3.0.0beta).
As this was my first contribution to Rails core, I needed to figure out how everything worked. The Contributing To Rails Guide is a great start and I was able to quickly fork Rails thanks to Github, clone it, make my patch, and then start running test cases. As soon as I tried to install the dependencies, I got an error trying to install the PostgreSQL gem, pg.
No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config
Oh, yeah, I don’t have PostgreSQL installed yet. I installed PostgreSQL from the one click installer at EnterpriseDB.
Next attempt at bundle install
(I am using Bundler instead of rubygems to install) gave me this:
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
The one click installer does not append the PostgreSQL bin directory to your path. You have two options, edit your bashrc and append the path permanently in there, or simply run gem
or bundle
with the PATH appended just before it like so:
PATH=$PATH:/Library/PostgreSQL/9.0/bin/ bundle install
or
PATH=$PATH:/Library/PostgreSQL/9.0/bin/ gem install pg
That should do the trick. pg
installed just fine after this and I was able to finish up my test cases and submit my patch!