Installing PostgreSQL and pg gem on Mac OSX

Posted March 12, 2011 under Rails

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! :D

If you liked this post, follow me on Twitter or subscribe to my RSS feed.

Discussion

  • Manuel

    Thank you very much! ;)

  • http://excid3.com Chris Oliver

    Glad to help! :D

  • http://profiles.google.com/enrico.stahn Enrico Stahn

    You made my day. Cheers.

  • 123

    Thanks a lot

  • Mike

    You are the man. Saved me so much frustration 

  • http://www.goldvinewebsolutions.com Matt Goldman

    I <3 you.

  • stew

    yeh fantastic. thanks.

  • Ryan

    only an hour of frustration because of this post! could have been forever it seemed…!!! thanks mate!

  • Richard Gonzales

    Thank you very much for sharing this! You really saved a lot of frustrating hours!

    BTW, for anybody who might stumble into this, here’s what I did:
    1. used macport to install PostGreSQL, sudo port install postgresql91 postgresql91-server
    2. used this to bundle: PATH=$PATH:/opt/local/lib/postgresql91/bin/ bundle install

    Cheers!

  • http://excid3.com Chris Oliver

    Thanks for the feedback Richard.

    I’d definitely suggest using homebrew over macports or fink instead: http://mxcl.github.com/homebrew/

    It compiles from source and has all the defaults you’ll likely want setup for you already.

  • LauraOjala

    Awesome, worked like magic, thanks!

  • bengm

    Bless your heart. Finally! Thank you.

    I can’t believe that this isn’t better documented / handled. I am doing everything the default way for a rails app (Mac, new versions of ruby/rails, deploy to heroku), and it is way too hard to set this up.

  • http://excid3.com Chris Oliver

    Glad to help! It’s sad that there is so much trouble around this gem!

  • http://deveiate.org/ Michael Granger

    If you (or @707f7bbbb1c65a63b4795f9d74feb45c:disqus ) have suggestions about how to make the ‘pg’ gem “better documented/handled”, I’d be happy to hear them. 

  • http://excid3.com Chris Oliver

    Any way you could catch some of the common errors and display appropriate text on how to fix them?

  • http://deveiate.org/ Michael Granger

    That’s what the “No pg_config… trying anyway. If building fails, please try again with –with-pg-config=/path/to/pg_config” is. 

    Maybe it should be more verbose? Or dump one of the READMEs (e.g., https://bitbucket.org/ged/ruby-pg/src/tip/README-OS_X.rdoc) if it detects you’re on an appropriate platform?

  • http://excid3.com Chris Oliver

    Yeah, I guess what I was thinking of, is more something like how passenger’s installation works. I love how it suggests hey you should check and make sure this file exists and what not.

    Maybe you could say “Here are some common paths that pg_config is located: ____. Make sure it’s in your path.” Or something similar.

    Just my two cents, aside from that though, it’s easy enough to find the solutions for problems when googling it.

    Also I wanted to say thanks for being and reaching out to help get suggestions for improvement! :)

  • http://deveiate.org/ Michael Granger

    Okay, thanks for the suggestion. It’s been a while since I installed Passenger, but I’ll look at how they’re doing things. 

  • Pingback: rake db:migrate not working on my postgres db