Error installing Postgres gem on Mac - ruby-on-rails

Here's the error I get when I run gem install pg:
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -r ./siteconf20151004-5579-1t2l557.rb 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 pg_config_manual.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 ***
If I run brew install postgres I get:
Warning: postgresql-9.4.4 already installed
Likewise, gem list shows pg (0.18.3) and a simple gem -v pg gives:
2.4.4
Running sudo ARCHFLAGS="-arch x86_64" gem install pg returns:
Building native extensions. This could take a while...
Successfully installed pg-0.18.3
invalid options: -f fivefish
(invalid options are ignored)
Parsing documentation for pg-0.18.3
Done installing documentation for pg after 4 seconds
1 gem installed
But bundle install throws up this error:
An error occurred while installing pg (0.18.3), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.3'` succeeds before bundling.
I'm seeing this in mkmf.log, which I think could be the biggest clue:
ld: warning: ignoring file /opt/local/lib/postgresql93/libpq.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/postgresql93/libpq.dylib
Undefined symbols for architecture i386:
"_PQconnectdb", referenced from:
_t in conftest-fc6c12.o
ld: symbol(s) not found for architecture i386
Which is strange given file /opt/local/lib/postgresql93/libpq.dylib returns x86_64, not i386:
/opt/local/lib/postgresql93/libpq.dylib: Mach-O 64-bit dynamically linked shared library x86_64
This didn't work either:
sudo env ARCHFLAGS="-arch i386 -arch x86_64" gem install pg

It seems you're almost there, because sudo ARCHFLAGS="-arch x86_64" gem install pg works. Try setting ARCHFLAGS globally, so bundle also can see it:
$ export ARCHFLAGS="-arch x86_64"
$ bundle

I've been working through this issue since yesterday. I ended up uninstalling Postgres, homebrew, and starting from scratch. When trying to add the 'pg' gem to my Ruby on Rail project it kept failing. Digging through the output I kept seeing it try to link the i386 libraries. So I tried adding the flag:
--arch-flags='x86_64'
That didn't seem to have any impact.
Then I found one where it was set right after the 'sudo'. That seemed to do the trick.
THIS WORKED!!!!!!!!
sudo ARCHFLAGS="-arch x86_64" gem install pg -v '0.18.3' -- --with-pg-config=/usr/local/bin/pg_config --arch-flags='x86_64'
In the project I actually had to repeat the steps for some reason in the project directory. Then re-repeat them and upgrade to version 0.18.4.
After that I could run 'bundle' and everything was included correctly.

A couple of the answers here:
http://stackoverflow.com/questions/19625487/impossible-to-install-pg-gem-on-my-mac-with-mavericks
suggest specifying the postgresql configuration file when performing the gem install. It is older but might provide the way forward. Search for "Check you have pg_config in the installed brew", which explains how to find the correct filename.

Related

Install PG gem in OpenSUSE

283
65
I'm trying to install PostgreSQL's pg gem for Ruby.
I issued the following command:
gem install pg
I installed Ruby 1.9.2 using RVM.
The above command shows me the following error.
The error is :
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/home/User/.rvm/rubies/ruby-1.9.2-preview3/bin/ruby extconf.rb
checking for pg_config... yes
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 ***
etc...
Somebody gave solution for Ubuntu users.
What about OpenSUSE Linux?
Thanks for advance.
For me this was the solution:
zypper in postgresql-server-devel
pg_config is part of postgresql-server-devel, if you install that
package then it should work on Tumbleweed at least.
Source: https://lists.opensuse.org/opensuse-factory/2019-03/msg00300.html
As per my search OpenSUSE linux has no package for client of Postgres, but as you have installed that it, that mean chances are the file pgsql.so is present in your system. How did you install it, I can see following which says it will install development libraries and client also
sudo zypper in postgresql postgresql-server postgresql-contrib
sudo zypper in postgresql-plperl postgresql-plpython postgresql-plctl
If this does not work then try to search in postgres installation path this file, locate file_name is easy to find files on different linux, but not sure it works in openSUSE it is part of mlocate package. If you find that file then symbol link it as per error, where error is searching this file.
If it is still not working, then you can use your Postgres directory in environment variable and try to install gem individually. To get postgres configuration use following command to see directories.
pg_config
Now try to install gem
PATH=$PATH:/Library/PostgreSQL/9.0/bin sudo gem install pg
On some system following command also resolve issue, so you can try it too
sudo gem install pg – --with-pg-config=/usr/local/pgsql/bin/pg_config

Gem install ffi Failed to build gem native extension

I'm having troubles to do bundle install in one project I get to work on in Rails 4.2.10. When I bundle I receive the next error:
An error occurred while installing ffi (1.9.25), and Bundler cannot continue.
Make sure that gem install ffi -v '1.9.25' succeeds before bundling.
In Gemfile:
s3_direct_upload was resolved to 0.1.6, which depends on
sass-rails was resolved to 5.0.7, which depends on
sass was resolved to 3.6.0, which depends on
sass-listen was resolved to 4.0.0, which depends on
rb-inotify was resolved to 0.9.10, which depends on
ffi
When I run gem install ffi -v '1.9.25' also an error occurs:
Building native extensions. This could take a while...
ERROR: Error installing ffi:
ERROR: Failed to build gem native extension.
current directory: /xxx/xxx/.rvm/gems/ruby-2.5.3/gems/ffi-1.9.25/ext/ffi_c
/xxx/xxx/.rvm/rubies/ruby-2.5.3/bin/ruby -r ./siteconf20190110-6923-1jimorr.rb extconf.rb
checking for ffi.h... no
checking for ffi.h in /usr/local/include,/usr/include/ffi... yes
checking for ffi_call() in -lffi... yes
checking for ffi_closure_alloc()... no
checking for ffi_raw_call()... no
checking for shlwapi.h... no
checking for ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_thread_call_with_gvl()... yes
checking for rb_thread_call_without_gvl()... yes
creating extconf.h
creating Makefile
current directory: /xxx/xxx/.rvm/gems/ruby-2.5.3/gems/ffi-1.9.25/ext/ffi_c
make "DESTDIR=" clean
current directory: /xxx/xxx/.rvm/gems/ruby-2.5.3/gems/ffi-1.9.25/ext/ffi_c
make "DESTDIR="
Configuring libffi
clang: error: unsupported option '-print-multi-os-directory'
clang: error: no input files
cd "/xxx/xxx/.rvm/gems/ruby-2.5.3/gems/ffi-1.9.25/ext/ffi_c/libffi-x86_64-darwin18" && /Volumes/Macintosh HD/Applications/Xcode.app/Contents/Developer/usr/bin/make
/bin/sh: /Volumes/Macintosh: No such file or directory
make: *** ["/xxx/xxx/.rvm/gems/ruby-2.5.3/gems/ffi-1.9.25/ext/ffi_c/libffi-x86_64-darwin18"/.libs/libffi_convenience.a] Error 127
make failed, exit code 2
Gem files will remain installed in /xxx/xxx/.rvm/gems/ruby-2.5.3/gems/ffi-1.9.25 for inspection.
Results logged to /xxx/xxx/.rvm/gems/ruby-2.5.3/extensions/x86_64-darwin-18/2.5.0/ffi-1.9.25/gem_make.out
I've been trying many solutions online but cannot resolve this issue.
Seems the issue is with my system as I'm new to the team and the other developers were unable to help me as they do not have this kind of issue.
I was able to successfully install ffi with the following command:
LDFLAGS="-L/usr/local/opt/libffi/lib" PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig" gem install ffi -v '1.9.25'
Found on some japanese board
According to the libffi README, only GCC is a tested compiler on macOS. You are using Clang, which doesn't support the command line option -print-multi-os-directory.
You need to either patch libffi to work with Clang, or use GCC.
Your error is the following Configuring libffi
clang: error: unsupported option '-print-multi-os-directory'
clang: error: no input files because Clang (wich is the C compiler you are using) doesn't support the option '-print-multi-os-directory'.
Could you check that X-Code command line tools are perfectly installed by executing the following command? xcode-select --install
Also would you mind to try brew install automake autoconf libtool libffi to install required dependencies?

Cannot install mysql2 gem for Rails project

I've looked at and tried just about everything on the topic here at Stackoverflow.com and still can't figure this out...
When I 'gem install mysql2' I get a permissions error. when I 'sudo gem install mysql2' I get the following:
Teds-MacBook-Pro:~ tedmartin$ sudo gem install mysql2
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
current directory: /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/ext/mysql2
/Users/tedmartin/.rbenv/versions/2.4.1/bin/ruby -r ./siteconf20170420-50202-1nekuvd.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql-connector-c/6.1.9/lib
-----
creating Makefile
current directory: /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -l-lpthread
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5 for inspection.
Results logged to /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-15/2.4.0-static/mysql2-0.4.5/gem_make.out
Anyone who has an idea, suggestion, tip, fix, etc... I'd be really grateful.
Thanks!
From the answer at this question, running the following worked for me
gem install mysql2 --source 'https://rubygems.org/' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
I ran into this same issue on Mojave, and none of the previous answers worked for me but this commands mentioned in this Github issue comment did
Step 1.
brew install openssl
Step 2.
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
In my case I already had openssl installed so it was really the export command that solved the problem.
Make sure openssl is installed on Mac via Homebrew.
brew install openssl
Install mysql2 gem.
gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
If the bug is not fixed, this above solution is the last one
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
Another solution if the above does not work.
cd /usr/local/Cellar/openssl/1.0.2s/lib/
sudo cp libssl.1.0.0.dylib libcrypto.1.0.0.dylib /usr/local/lib
This worked for me on macOS Catalina:
Make sure you have openssl installed. If not:
brew install openssl
Then do the gem install:
gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)"
Try this:
which mysql
Then, use the output-result to install the gem using the existing mysql installation directory:
gem install mysql2 -- --with-mysql-dir=<mysql-installation-directory>
It away working for me after 2h crazy
Unstall Ruby and Install Ruby v2.6.6
Run command line as Administrator gem install mysql2
Install rails gem install rails
Create new project rails new my_proj -d mysql
Work 100% in Windown

Error while building bundle due to pg gem? [duplicate]

I just upgraded from OS X Lion to OS X Mountain Lion.
My rails environment for the most part stayed in tact.
The only steps I had to do to get things back to a good state was:
- Install XCode 4.4
- Install XCode 4.4 Command Line Tools
- Install gcc-4.2
So all that went well.
Here's the issue, almost all my gems install properly when I run bundle install except for one of the most important ones I need - the postgresql gem 'pg'.
And it specifically fails for versions above 0.12.2.
I'm using ruby 1.9.3p125.
I can successfully grab the 0.12.2 pg gem, and versions below, but they give me segmentation faults (I'm guessing because of the version of ruby I'm using).
So this leads me to believe that it's specific to the pg gem, and not my overall environment seeing as other gems install fine.
I also tried re-installing Postgresql to ensure my postgres environment is ok and seems good on that front.
Has anybody been able to install the 'pg' gem version 0.13.0 or above (and more preferrably 0.14.X)?
Here is my stack trace:
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/caseyli/.rvm/rubies/ruby-1.9.3-p125/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconnectionUsedPassword()... yes
checking for PQisthreadsafe()... yes
checking for PQprepare()... yes
checking for PQexecParams()... yes
checking for PQescapeString()... yes
checking for PQescapeStringConn()... yes
checking for PQescapeLiteral()... yes
checking for PQescapeIdentifier()... yes
checking for PQgetCancel()... yes
checking for lo_create()... yes
checking for pg_encoding_to_char()... yes
checking for pg_char_to_encoding()... yes
checking for PQsetClientEncoding()... yes
checking for PQlibVersion()... yes
checking for PQping()... yes
checking for rb_encdb_alias()... yes
checking for rb_enc_alias()... no
checking for PGRES_COPY_BOTH in libpq-fe.h... no
checking for struct pgNotify.extra in libpq-fe.h... yes
checking for unistd.h... yes
checking for ruby/st.h... yes
creating extconf.h
creating Makefile
make
compiling pg.c
pg.c: In function ‘pg_s_library_version’:
pg.c:273: warning: implicit declaration of function ‘PQlibVersion’
pg.c: In function ‘Init_pg_ext’:
pg.c:384: error: ‘PQPING_OK’ undeclared (first use in this function)
pg.c:384: error: (Each undeclared identifier is reported only once
pg.c:384: error: for each function it appears in.)
pg.c:386: error: ‘PQPING_REJECT’ undeclared (first use in this function)
pg.c:388: error: ‘PQPING_NO_RESPONSE’ undeclared (first use in this function)
pg.c:390: error: ‘PQPING_NO_ATTEMPT’ undeclared (first use in this function)
make: *** [pg.o] Error 1
Gem files will remain installed in /Users/caseyli/.rvm/gems/ruby-1.9.3- p125#elliottfarmequipment/gems/pg-0.14.0 for inspection.
Deefour's comment got it! It was the way I installed PostgreSQL.
I use the PostgreSQL one-click installer which I'm guessing doesn't set up the libpq up properly for Mountain Lion.
Homebrew on the otherhand will build it properly for the OS.
After installing postgres through homebrew
brew install postgresql
I was able to install my gem no problem.
Thanks everybody!
The issue is the location of pg_config. If you use the one-click installer, it is in /Library/PostgreSQL/9.2/bin/pg_config, so this will build your gem just fine:
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Library/PostgreSQL/9.2/bin/pg_config
I had the exact same issue; same stack trace as you've posted.
MRI 1.9.3-p286 / OS X 10.8.2 / Postgres 9.0.4 with homebrew.
I solved it by simply doing:
$ brew update
$ brew upgrade postgresql
$ bundle
Hope it works for someone else.
Reinstalling ruby fixed a similar issue I was having after upgrading to Mountain Lion
rvm reinstall 1.9.3
Note: also run the following as it seems the default version resets
rvm --default 1.9.3
For reference I posted the error I was seeing below
gem install pg
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 --with pg=/usr/local/Cellar/postgresql/9.1.4/bin
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Mac comes with an outdated prebuilt PostgreSQL client installation and pg_config file that comes with it conflicts with any new PostgreSQL server & client installations.
Installing ruby gem with a fully qualified file name of pg_config overrides default known location of pg_config and installation succeeds.
Current location of pg_config by one click installer by EnterpriseDB: /Library/PostgreSQL/bin/pg_config
This command finally worked for me.
CrashMX2$ gem install pg -- --with-pg-config=/Library/PostgreSQL/bin/pg_config
I have had the same problem but reinstalling postgresql with homebrew did not solve it. However, this command did it for me:
sudo env ARCHFLAGS="-arch x86_64" gem install pg
I was able to get it to work by installing the Postgres.app from Heroku and removing my existing postgresql installation. You can download the app here:
http://postgresapp.com/
brew remove postgresql
bundle install
If you didn't use brew, here's a link to more uninstall docs from Postgres.app
http://postgresapp.com/documentation#toc_16
I just got mine up and running via this ticket:
How can I pass a parameter for gem installation when I run bundle install?
bundle config build.pg --with-pg-config=/path/to/pg_config
bundle install
Like Matt, Ranjan, and Peter Mellett all say, you have to get the right pg_config location, and Mountain Lion comes with a different location that doesn't seem to play well with rubygems, so if you find your old version like
/Library/PostgreSQL/bin/pg_config
then you can set that in the bundle config before installing.
My situation was upgrading from Lion to Mountain Lion.
Homebrew installed pg_ctl in /usr/local/bin/pg_ctl, so the command for me was:
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_ctl
Fro those, who are using Kubuntu 13.04 and installed pg with one click tool, pg config is in other folder, so command:
gem install pg -- --with-pg-config=/opt/PostgreSQL/9.3/bin/pg_config

pg gem '0.14.0' on Mountain Lion fails

I just upgraded from OS X Lion to OS X Mountain Lion.
My rails environment for the most part stayed in tact.
The only steps I had to do to get things back to a good state was:
- Install XCode 4.4
- Install XCode 4.4 Command Line Tools
- Install gcc-4.2
So all that went well.
Here's the issue, almost all my gems install properly when I run bundle install except for one of the most important ones I need - the postgresql gem 'pg'.
And it specifically fails for versions above 0.12.2.
I'm using ruby 1.9.3p125.
I can successfully grab the 0.12.2 pg gem, and versions below, but they give me segmentation faults (I'm guessing because of the version of ruby I'm using).
So this leads me to believe that it's specific to the pg gem, and not my overall environment seeing as other gems install fine.
I also tried re-installing Postgresql to ensure my postgres environment is ok and seems good on that front.
Has anybody been able to install the 'pg' gem version 0.13.0 or above (and more preferrably 0.14.X)?
Here is my stack trace:
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/caseyli/.rvm/rubies/ruby-1.9.3-p125/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconnectionUsedPassword()... yes
checking for PQisthreadsafe()... yes
checking for PQprepare()... yes
checking for PQexecParams()... yes
checking for PQescapeString()... yes
checking for PQescapeStringConn()... yes
checking for PQescapeLiteral()... yes
checking for PQescapeIdentifier()... yes
checking for PQgetCancel()... yes
checking for lo_create()... yes
checking for pg_encoding_to_char()... yes
checking for pg_char_to_encoding()... yes
checking for PQsetClientEncoding()... yes
checking for PQlibVersion()... yes
checking for PQping()... yes
checking for rb_encdb_alias()... yes
checking for rb_enc_alias()... no
checking for PGRES_COPY_BOTH in libpq-fe.h... no
checking for struct pgNotify.extra in libpq-fe.h... yes
checking for unistd.h... yes
checking for ruby/st.h... yes
creating extconf.h
creating Makefile
make
compiling pg.c
pg.c: In function ‘pg_s_library_version’:
pg.c:273: warning: implicit declaration of function ‘PQlibVersion’
pg.c: In function ‘Init_pg_ext’:
pg.c:384: error: ‘PQPING_OK’ undeclared (first use in this function)
pg.c:384: error: (Each undeclared identifier is reported only once
pg.c:384: error: for each function it appears in.)
pg.c:386: error: ‘PQPING_REJECT’ undeclared (first use in this function)
pg.c:388: error: ‘PQPING_NO_RESPONSE’ undeclared (first use in this function)
pg.c:390: error: ‘PQPING_NO_ATTEMPT’ undeclared (first use in this function)
make: *** [pg.o] Error 1
Gem files will remain installed in /Users/caseyli/.rvm/gems/ruby-1.9.3- p125#elliottfarmequipment/gems/pg-0.14.0 for inspection.
Deefour's comment got it! It was the way I installed PostgreSQL.
I use the PostgreSQL one-click installer which I'm guessing doesn't set up the libpq up properly for Mountain Lion.
Homebrew on the otherhand will build it properly for the OS.
After installing postgres through homebrew
brew install postgresql
I was able to install my gem no problem.
Thanks everybody!
The issue is the location of pg_config. If you use the one-click installer, it is in /Library/PostgreSQL/9.2/bin/pg_config, so this will build your gem just fine:
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Library/PostgreSQL/9.2/bin/pg_config
I had the exact same issue; same stack trace as you've posted.
MRI 1.9.3-p286 / OS X 10.8.2 / Postgres 9.0.4 with homebrew.
I solved it by simply doing:
$ brew update
$ brew upgrade postgresql
$ bundle
Hope it works for someone else.
Reinstalling ruby fixed a similar issue I was having after upgrading to Mountain Lion
rvm reinstall 1.9.3
Note: also run the following as it seems the default version resets
rvm --default 1.9.3
For reference I posted the error I was seeing below
gem install pg
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 --with pg=/usr/local/Cellar/postgresql/9.1.4/bin
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Mac comes with an outdated prebuilt PostgreSQL client installation and pg_config file that comes with it conflicts with any new PostgreSQL server & client installations.
Installing ruby gem with a fully qualified file name of pg_config overrides default known location of pg_config and installation succeeds.
Current location of pg_config by one click installer by EnterpriseDB: /Library/PostgreSQL/bin/pg_config
This command finally worked for me.
CrashMX2$ gem install pg -- --with-pg-config=/Library/PostgreSQL/bin/pg_config
I have had the same problem but reinstalling postgresql with homebrew did not solve it. However, this command did it for me:
sudo env ARCHFLAGS="-arch x86_64" gem install pg
I was able to get it to work by installing the Postgres.app from Heroku and removing my existing postgresql installation. You can download the app here:
http://postgresapp.com/
brew remove postgresql
bundle install
If you didn't use brew, here's a link to more uninstall docs from Postgres.app
http://postgresapp.com/documentation#toc_16
I just got mine up and running via this ticket:
How can I pass a parameter for gem installation when I run bundle install?
bundle config build.pg --with-pg-config=/path/to/pg_config
bundle install
Like Matt, Ranjan, and Peter Mellett all say, you have to get the right pg_config location, and Mountain Lion comes with a different location that doesn't seem to play well with rubygems, so if you find your old version like
/Library/PostgreSQL/bin/pg_config
then you can set that in the bundle config before installing.
My situation was upgrading from Lion to Mountain Lion.
Homebrew installed pg_ctl in /usr/local/bin/pg_ctl, so the command for me was:
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_ctl
Fro those, who are using Kubuntu 13.04 and installed pg with one click tool, pg config is in other folder, so command:
gem install pg -- --with-pg-config=/opt/PostgreSQL/9.3/bin/pg_config

Resources