I just upgraded to rails3 and had a bit of a mess with macports/gems/databases to sort out. So I threw out all gems to install them fresh. Everything seems to be ok except for the aquirement of the pg gem.

After creating a new rails3 project, prepared for postgres, the server would not start, complaining about the missing pg gem. Ok, doing bundle install then. It chugs along for a while and, of course, fails on the pg gem!

Any pointers welcome. Any missing information that would help figuring this out, I'll gladly and promptly provide! Thanks.

/me

EDIT:

I downloaded xcode etc (since the dvd version is said to be broken) and things went better for a while. bundle install started talking about archflags instead. So i did:
sudo env ARCHFLAGS="-arch i386" bundle install and things seemed to go fine - until starting rails barfed, saying:

This kind of error usually means you haven't installed the development packages for the relevant software, in this case PG.
–
karmajunkieDec 8 '10 at 22:59

The XCode that came with Snow Leopard on the DVD was broken, which is why we need to update it.
–
the Tin ManDec 9 '10 at 5:20

Snow Leopard is 64-bit so you have to use x86_64. If you've installed anything and forced i386 you'll need to recompile it as x86_64 otherwise you will see the message you got. That includes any gems which were compiled/installed prior to installing Snow Leopard if you were running Leopard on that machine previously.
–
the Tin ManDec 9 '10 at 19:15

You'll need to do a locate libpq and find where it is. It is probably in the lib directory of your Postgres install. Then scan through the README and/or INSTALL doc for the gem and see if there's a flag needed, similar to the one for "include" and "lib". Search the googles for "ruby mac os libpq" and a lot of threads will show up with info; Others have encountered this, it's one of the more complicated parts of integrating Ruby with a service, but well worth it once it's done. I love working with Postgres and prefer it over the alternatives.
–
the Tin ManDec 9 '10 at 19:20

FWIW, I'm using homebrew and rbenv, and saw this when trying to gem install pg: Can't find the PostgreSQL client library (libpq). An old PostgreSQL was the issue. I had to rm -rf /Library/Postgre* before the homebrew-installed pg_config was seen.
–
mrmMar 12 '12 at 0:36

8 Answers
8

Generally the gem bundles for Postgres want to know where pg_config is hiding so they can ask about the Postgres installation.

Use locate pg_config to see if your Mac knows where it's hiding.

I installed a copy of Postgres using mappstack, so my Mac says there's a copy at:

/Applications/mappstack-1.2-3/postgresql/bin/pg_config

and another at:

/Library/PostgreSQL/9.0/bin/pg_config

I don't remember installing the one at /Library/PostgreSQL/9.0, so it might have been preinstalled by Snow Leopard, or I did it when under the influence of too much work, possibly using the Postgres installer from EnterpriseDB.

Once you've found the location of pg_config try adding that directory to the start of your PATH and then rerun the gem install.
Or use:

The next thing the installers might want are access to the Postgres headers, so you look in the parent of the bin directories, and see if you can find an include directory.

After that, look in that directory for a lib directory. Once you know those locations you should have all you need to set your environment variables to let the installer complete. You'll need to read the README or INSTALL file of the installer and see what needs to be set up. You'll be configuring:

Thanks! I came a bit further (edited original post) and maybe there is now just some pg lib path to supply as you desribed. Maybe I'm getting close? :)
–
npupDec 9 '10 at 10:01

The above solution helped me on another computer in what first appeared to be in the same state, so it was a working solution, thanks. Still fighting with the main computer where something more is lacking..
–
npupJan 3 '11 at 13:35

Re-edit your original question and add symptoms you're seeing. It's most likely something is missing still. I was just doing some development using my Postgres and Rails3 so I know it's possible to get it working.
–
the Tin ManJan 3 '11 at 13:40

I used the PostGRE installer from the postgre website.
–
MosheJul 6 '11 at 22:42

1

"or I did it when under the influence of too much work" :-) Didn't your momma tell you never to visit that bad neighborhood where Too Much Work used to hang out?
–
thomaxMay 14 '12 at 18:25

This one worked for me. The fix selected for this question worked for installing pg via gem, but bundle still chocked. Setting ARCHFLAGS was the fix on 10.7 and PostgreSQL installed via Homebrew.
–
cmhobbsJul 25 '11 at 15:48

You might want to try going with homebrew for installing postgres (brew install postgres) and Ruby Version Manager for installing and maintaining ruby and ruby gems.

It'll leave your default versions (installed with OSX) untouched and give you more flexibility. For example, you could have different rails apps using rails 2 or 3 with ruby 1.8.7 and 1.9.2 all installed on the same system without problems.

Thanks. I'm a bit wary about fiddling with postgres since this is my dev machine for work (we use postgres there, and it's well set up for its purposes). Maybe it doesn't matter? I don't know how homebrew works (yet).
–
npupDec 9 '10 at 9:59

I'd encourage you to check it out. Homebrew installs files under its own filepath (ie, /usr/local/Cellar/postgresql/) and then symlinks the binary to /usr/local/bin for easy access (and doesn't touch anything that's preinstalled). Even if you don't use homebrew though, you should still check out RVM. It'll probably save you a lot of trouble with managing gems.
–
markquezadaDec 9 '10 at 20:06

Homebrew is brilliant - saved my sanity a couple of times now. RVM is a must for any ruby/rails developer.
–
jjnevisSep 23 '11 at 10:25

Generally the gem bundles for Postgres want to know where pg_config is hiding . . .

Right - this could be the trick. If you just installed PostgreSQL and added it to your path, and did "bundle install" in an old shell, it won't be able to find pg_config. If that's the case, just get a new shell and try again. Otherwise, follow the steps above to find it and get it seen by bundler.

The macports install for postgresql83 works fine and plays nicely with rails right out of the box - that's another way to do it.