Can’t find the PostgreSQL client library (libpq)

You might encounter this error while trying to install the pg gem (v0.10.1), or when updating from v0.10.

My stacktrace on Mac OS X Snow Leopard 10.6.6 with Rubygems 1.4.2, Ruby 1.8.7 and Postgres 9.0.2 was the following:

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
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Oookay, so let’s try to find out what’s going wrong. A quick Google search shows that errors like this one happens on Linux too, and usually suggests that ARCHFLAGS definitions may be a problem. Indeed, /Library/Ruby/Gems/1.8/gems/pg-0.10.1/ext/mkmf.log contains this error message:

ld: warning: in /usr/local/Cellar/postgresql/9.0.2/lib/libpq.dylib, file was built for unsupported file format which is not the architecture being linked (i386)

The problem seems to come from the way Homebrew installs Postgres.

Homebrew install notes include the following informative message:

If you want to install the postgres gem, including ARCHFLAGS is recommended:
    env ARCHFLAGS="-arch x86_64" gem install pg

There! Now the only problem is if you install your gems as root, you can’t merely put a sudo before the “gem”, but you have to sudo su then execute the command:

Air:~ florent$ sudo su
Password:
sh-3.2# env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions.  This could take a while...
Successfully installed pg-0.10.1
1 gem installed
Installing ri documentation for pg-0.10.1...
Installing RDoc documentation for pg-0.10.1...

Voilà!

Edit 18 Dec. Chad provided a simpler solution in the comments that doesn’t require you to log in as root and works just as well:

sudo env ARCHFLAGS="-arch x86_64" gem install pg

14 thoughts on “Can’t find the PostgreSQL client library (libpq)”

  1. Great Post! It looked exactly like my problem on a google, but when I tried it the solution didn’t work for me. My problem was that the pg_config file being located was the wrong, outdated one. From the results of “gem install pg”:

    checking for pg_config… yes
    Using config values from /usr/local/bin/pg_config
    checking for libpq-fe.h… yes
    checking for libpq/libpq-fs.h… yes
    checking for PQconnectdb() in -lpq… no
    checking for PQconnectdb() in -llibpq… no
    checking for PQconnectdb() in -lms/libpq… no
    Can’t find the PostgreSQL client library (libpq)

    It was using the older pg_config so that none of the modern libs could be found. The valid one, just installed by the EnterpriseDB binary installer was here:

    /Library/PostgreSQL/9.0/bin/

    and that’s the one I wanted to use, so I did a little sleight of hand:

    “PATH=$PATH:/Library/PostgreSQL/9.0/bin/” at the command line, then

    “mv /usr/local/bin/pg_config /usr/local/bin/pg_config_orig” to rename the old file

    after that:

    “gem install pg”

    Cheers,

    Bill

  2. You can however wrap the entire statement with sudo with the desired effect without logging in as root. e.g. sudo env ARCHFLAGS=”-arch x86_64″ gem install pg

  3. I don’t think you need the postgres development files, everything you need should have been included with your installer. It’s more likely that the path they’re installed to isn’t in your environment path and therefore gem can’t find them when it tries to compile pg.

Leave a Reply

Your email address will not be published. Required fields are marked *