Can't find the PostgreSQL client library (libpq)
Ruby on-RailsRuby on-Rails-3PostgresqlRubygemsRuby on-Rails Problem Overview
I'm trying to install PostgreSQL for Rails on Mac OS X 10.6. First I tried the MacPorts install but that didn't go well so I did the one-click DMG install. That seemed to work.
I suspect I need to install the PostgreSQL development packages but I have no idea how to do that on OS X.
Here's what I get when I try to do sudo gem install pg
:
$ sudo 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
checking for pg_config... yes
Using config values from /Library/PostgreSQL/8.3/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.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
--with-pg
--without-pg
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pqlib
--without-pqlib
--with-libpqlib
--without-libpqlib
--with-ms/libpqlib
--without-ms/libpqlib
Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/pg-0.11.0 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/pg-0.11.0/ext/gem_make.out
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
$ sudo su
$ env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions. This could take a while...
Successfully installed pg-0.11.0
1 gem installed
Installing ri documentation for pg-0.11.0...
Installing RDoc documentation for pg-0.11.0...
WORKED!
Solution 2 - Ruby on-Rails
I tried the top-rated answer here:
env ARCHFLAGS="-arch x86_64" gem install pg
But when I tried running bundle install again, it had the same error. Then I tried the entire bundle install with ARCHFLAGS like so:
ARCHFLAGS="-arch x86_64" bundle install
Worked for me! Make sure to replace x86_64 with i386 depending on what architecture you have.
Solution 3 - Ruby on-Rails
I was just having this problem when using the EnterpiseDB .dmg. If that's the same think you used, I got it to work by specifying the right architecture:
sudo env ARCHFLAGS="-arch i386" gem install pg
There are some tutorials on the web that said to specify a different architecture (like "-arch x86_64" for people who used MacPorts) but it wasn't working for me because I used the single file install.
Solution 4 - Ruby on-Rails
If using Yosemite:
brew install postgres
Then:
ARCHFLAGS="-arch x86_64" gem install pg
And (optional) finally, if you want to launch autovacuum...
postgres -D /usr/local/var/postgres
Solution 5 - Ruby on-Rails
Maybe you can try this one:
ARCHFLAGS="-arch i386 -arch x86_64" gem install pg
To know the architecture of your library you can use
file /usr/local/lib/libpq.dylib
which gave just 1 architecture in my case (installed via homebrew):
/usr/local/lib/libpq.dylib: Mach-O 64-bit dynamically linked shared library x86_64
Solution 6 - Ruby on-Rails
Solution: reinstalled PostgreSQL with Homebrew.
Solution 7 - Ruby on-Rails
The error message is right there:
Can't find the PostgreSQL client library (libpq)
You can fix that by using homebrew to install it
brew install libpq
The following, or a similar, message will appear:
If you need to have libpq first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
For compilers to find libpq you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"
You can then run it:
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
gem install pg
Done.
Solution 8 - Ruby on-Rails
Fake out gem
by prefixing the appropriate environment variables. If you were installing from MacPorts, you should be able to walk through the following procedure:
% /opt/local/lib/postgresql91/bin/pg_config
BINDIR = /opt/local/lib/postgresql91/bin
DOCDIR = /opt/local/share/doc/postgresql
HTMLDIR = /opt/local/share/doc/postgresql
INCLUDEDIR = /opt/local/include/postgresql91
PKGINCLUDEDIR = /opt/local/include/postgresql91
INCLUDEDIR-SERVER = /opt/local/include/postgresql91/server
LIBDIR = /opt/local/lib/postgresql91
PKGLIBDIR = /opt/local/lib/postgresql91
LOCALEDIR = /opt/local/share/locale
MANDIR = /opt/local/share/man
SHAREDIR = /opt/local/share/postgresql91
SYSCONFDIR = /opt/local/etc/postgresql91
PGXS = /opt/local/lib/postgresql91/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/opt/local' '--sysconfdir=/opt/local/etc/postgresql91' '--bindir=/opt/local/lib/postgresql91/bin' '--libdir=/opt/local/lib/postgresql91' '--includedir=/opt/local/include/postgresql91' '--datadir=/opt/local/share/postgresql91' '--mandir=/opt/local/share/man' '--with-includes=/opt/local/include' '--with-libraries=/opt/local/lib' '--with-openssl' '--with-bonjour' '--with-readline' '--with-zlib' '--with-libxml' '--with-libxslt' '--enable-thread-safety' '--enable-integer-datetimes' '--with-ossp-uuid' 'CC=/usr/bin/gcc-4.2' 'CFLAGS=-pipe -O2 -arch x86_64' 'LDFLAGS=-L/opt/local/lib -arch x86_64' 'CPPFLAGS=-I/opt/local/include -I/opt/local/include/ossp'
CC = /usr/bin/gcc-4.2
CPPFLAGS = -I/opt/local/include -I/opt/local/include/ossp -I/opt/local/include/libxml2 -I/opt/local/include
CFLAGS = -pipe -O2 -arch x86_64 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wformat-security -fno-strict-aliasing -fwrapv
CFLAGS_SL =
LDFLAGS = -L/opt/local/lib -arch x86_64 -L/opt/local/lib -L/opt/local/lib -Wl,-dead_strip_dylibs
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgport -lxslt -lxml2 -lssl -lcrypto -lz -lreadline -lm
VERSION = PostgreSQL 9.1beta1
From there, pull out the LIBDIR
, INCLUDEDIR
, CPPFLAGS
, LIBS
and LDFLAGS
(the one that I think will get you running is LIBDIR
, however). Then you'd run:
setenv PATH /opt/local/lib/postgresql91/bin:${PATH}
sudo env LDFLAGS=-L`pg_config --libdir` CPPFLAGS=`pg_config --cppflags` gem install pg
That should do it for you. Let me know if it doesn't.
Solution 9 - Ruby on-Rails
The problem we had was pretty weird.
ruby -v # was ok (rbenv)
gem -v # was ok (rbenv)
but when we did a bundle install in fact, bundler wasn't installed for the version of ruby that was installed by rbenv, so, when we typed bundle install, it used the bundler of the system.
So before running bundle install, be sure that you have installed bundler by running
gem install bundler
Solution 10 - Ruby on-Rails
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.
You shouldn't have to run gem install pg
as root, in fact if you do it's likely your PATH (root's PATH if run w/ sudo) won't contain the necessary info.
The following usually works for me:
# Might be different depending on where your installer installed postgres 8.3
export PATH=$PATH:/Library/PostgreSQL/8.3/include/
export ARCHFLAGS='-arch x86_64'
gem install pg
Solution 11 - Ruby on-Rails
This is what finally did it for me (combination of multiple solutions provided before along with other posts):
$ sudo env ARCHFLAGS="-arch x86_64" gem install pg -- with-pg-include=/Library/PostgreSQL/9.6/include/
Solution 12 - Ruby on-Rails
Most of those answers focus on giving you one command that just works. However they only work for some particular scenarii. If you have trouble figuring out which command you should run, you better understand what is happening.
Why doesn't it work?
The pg
gem is a native extension written in C. It relies on the libpq
library which is the PostgreSQL library for C applications.
So, during its build, the pg
gem needs to be able to find the library libqp
compiled for the same architecture (for example x86_64
or arm64
).
There can be a mismatch if:
- you are on a m1 macbook (apple silicon) but have installed
postgres
in rosetta 2 mode. - you are on a 64-bit computer but have installed
postgres
in 32-bit mode.
If you end up in this situation, you have several options:
- try to compile the
pg
gem in the same architecture aspostgres
- reinstall postgres to match the architecture in which the pg
gem
is built (by default it's the same asruby
's)
Example
For instance, in my case, I have a m1 macbook.
I have installed ruby in "rosetta 2" mode (x86_64
) and postgres in "native" mode (arm64
).
You can check the architecture with the lipo -info
command:
rbenv which ruby
=> /Users/leo/.rbenv/versions/2.6.6/bin/ruby
lipo -info /Users/leo/.rbenv/versions/2.6.6/bin/ruby
=> /Users/leo/.rbenv/versions/2.6.6/bin/ruby is architecture: x86_64
which postgres
=> /opt/homebrew/bin/pg_config
lipo -info /opt/homebrew/bin/pg_config
=> Non-fat file: /opt/homebrew/bin/pg_config is architecture: arm64
To solve this, in my case, I just had to add to the PATH
another version of postgres
compiled in x86_64
. It allowed the pg
gem to be able to find binaries in the right architecture:
export PATH=/usr/local/Cellar/postgresql/13.2_1/bin:$PATH
gem install pg
# and finally ๐
=> Building native extensions. This could take a while...
=> Successfully installed pg-1.2.3
=> Parsing documentation for pg-1.2.3
=> Installing ri documentation for pg-1.2.3
=> Done installing documentation for pg after 1 seconds
=> 1 gem installed
Solution 13 - Ruby on-Rails
libpq from Postgres.app will not work on ARM-based Macs, as Postgres.app is not getting released for ARM (only Universal Intel/ARM packages get released, which is just x86_64 emulation on ARM64):
~ > file /Applications/Postgres.app/Contents/Versions/13/lib/libpq.dylib
/Applications/Postgres.app/Contents/Versions/13/lib/libpq.dylib: Mach-O 64-bit dynamically linked shared library x86_64
For this reason, I decided to install libpq separately from Homebrew:
brew install libpq
Then installing pg worked with this:
gem install pg -- --with-pq-dir=/opt/homebrew/opt/libpq/
(MacOS 11.6 Big Sur on MacBook Pro 13" 2020 with Apple M1 CPU)
Solution 14 - Ruby on-Rails
The ARCHFLAGS
answer that others have proposed will not work if you somehow ended up with a 64-bit version of postgres (which homebrew will install) and a 32-bit version of ruby. For some reason rbenv
insists on building ruby 1.9.2-p290 as 32-bit for me, which makes it impossible to link against 64-bit postgres.
Check the architecture of your ruby binary with
file `which ruby`
or if using rbenv
file `rbenv which ruby`
And compare against your postgres:
file `which postgres`
If there's a mis-match you'll need to re-install postgres or ruby. With rbenv I solved this just by switching to a different version: 1.9.3-p194
instead of 1.9.2-p290
.
Solution 15 - Ruby on-Rails
This is how I made it to work on Mavericks. Note: I already had installed postgresql 9.3 from homebrew.
-
Update Xcode to 5.0 from App Store
-
Install command line developer tools
xcode-select --install
-
Agree to Xcode license
sudo xcodebuild -license
-
Install gem
ARCHFLAGS="-arch x86_64" gem install pg
Solution 16 - Ruby on-Rails
So basically I did this ;-)
brew install postgres
Solution 17 - Ruby on-Rails
For my mac m1 after trying all of above solution and nothing working. I made it work by:
-
Install Homebrew x86
- brew x86 will locate on: /usr/local/Homebrew/bin/brew
- brew arm64 locate on: /opt/homebrew/bin/brew (my current brew)
-
Install libpq with arch x86 ( Since pg must be compiled with x86 libpq )
arch --x86_64 /usr/local/Homebrew/bin/brew install libpq
-
Install gem pg
gem install pg -v '0.18.2' -- --with-pq-dir=/usr/local/Cellar/libpq/14.2 --with-pg-config=/usr/local/Cellar/libpq/14.2/bin/pg_config
Solution 18 - Ruby on-Rails
Well, I have tried all the solutions given above for
> Can't find the PostgreSQL client library (libpq)
> >$ sudo su >$ env ARCHFLAGS="-arch x86_64" gem install pg >
but nothing helped. For this reason, I decided to install libpq separately from Homebrew:
brew install libpq
then found a small catch though: libpq won't install itself in the /usr/local/bin directory for mac.
To make that happen, you need to run:
brew link --force libpq
Which will symlink all the tools, not just libpq, into the /usr/local/bin directory. You're ready to run psql and start connecting now.
Solution 19 - Ruby on-Rails
If you are getting this error in Mac M1 pro, Silicon chip
Issue:
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)
Solution:
Install libpq via homebrew and add it to the PATH
brew install libpq
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
OR ~/bash_profile if you use it instead.
Try:
bundle install # OR
gem install pg
This works for me.
Solution 20 - Ruby on-Rails
I'm probably a bit late to the party here, but in my case I was using rbenv and upgrading to Ruby 2.2.3. I had to install Bundler to get mine to work, I had an old system version.
gem install bundler
Solution 21 - Ruby on-Rails
As mentioned above this has to do with the fact of having two ruby archs on rbenv /usr/bin/ruby: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [i386:Mach-O executable i386]
what i had to do was simply install pg
gem forcing x86_64
arch to be used with this command:
sudo env ARCHFLAGS="-arch x86_64" gem install pg
Remember to have your bash_profile
up to date
Add the path of your postgres, in this case im using Postgres app (OSX
) instead of brew
(https://postgresapp.com/) by default this is the location:
export PATH=/Applications/Postgres.app/Contents/Versions/10/bin:$PATH
Reload bash with
sudo vi ~/.bash_profile
After doing this i was able to finally successfully install pg gem
Hope this helps!
Solution 22 - Ruby on-Rails
On Mac you can try this (works for me): gem install pg -- with-pg-include=/Library/PostgreSQL/9.5/include Fetching: pg-1.0.0.gem (100%) Building native extensions with: 'with-pg-include=/Library/PostgreSQL/9.5/include' This could take a while... Successfully installed pg-1.0.0 Parsing documentation for pg-1.0.0 Installing ri documentation for pg-1.0.0 Done installing documentation for pg after 3 seconds 1 gem installed
(this part "/Library/PostgreSQL/9.5/include" you must put your Postgres path)
Solution 23 - Ruby on-Rails
For those of you that tried the top answers and they didn't quite work, I thought I'd explain my situation and how it was fixed.
I was trying to use postgres for a Ruby project, using bundle install
(and with a gem 'pg'
in my gemfile), but I already had Postgres installed before, probably by using npm install pg
or something like that.
I had an existing database, with data, and I was concerned that if I ran brew install postgres
then it would wipe my existing data and tables.
It didn't! brew install postgres
worked perfectly as suggested by @Jackie Chan
If you want to put your mind at ease though, you can back up your data as described at this link https://www.tecmint.com/backup-and-restore-postgresql-database/ e.g.
cd desktop
pg_dump -U your_db_user your_database_name >> file_containing_backup.sql