169

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
Leopd
  • 41,333
  • 31
  • 129
  • 167
Jason Swett
  • 43,526
  • 67
  • 220
  • 351

23 Answers23

356
$ 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!

Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
Siddhartha Mukherjee
  • 6,138
  • 3
  • 23
  • 11
  • 2
    I had to add the version "env ARCHFLAGS="-arch x86_64" gem install -v '0.14.0'" so that it worked with with my project – Matthieu Rouif Jun 25 '13 at 21:56
  • 7
    Worked for OS X Mavericks. For me, it's `env ARCHFLAGS="-arch x86_64" gem install pg -v '0.17.1' -- --with-pg-config=/opt/local/lib/postgresql91/bin/pg_config` – Dawei Yang Apr 29 '14 at 03:18
  • 1
    This worked for me. I am using OSX 10.9.3 with Postgres app 9.3 sudo env ARCHFLAGS="-arch i386" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config – Nathan Noble Jun 29 '14 at 08:51
  • 18
    And if you use "Postgres.app", under mavericks, you should do : env ARCHFLAGS="-arch x86_64 gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config (Assume you use Postgres.app version 9.3, because the location of pg_config maybe changed if you use different version, but anyway you can find it) – Zhaonan Jul 12 '14 at 19:34
  • 2
    This worked for me and broke a 2 hour stoppage. I'm new to rails and this was frustrating. Like the previous commenter, I was using the Postgres.app installer on Mavericks. Did not realize the ARCHFLGS env variable was so important. [Explanation here.](http://deveiate.org/code/pg/README-OS_X_rdoc.html#documentation) – GNat Jul 20 '14 at 21:05
  • Also, if you're using Bundler, env ARCHFLAGS="-arch x86_64" bundle will work correctly so you don't have to install the gem globally. – gleenn Aug 06 '14 at 16:31
  • YangDawei's answer worked for me by modifying the path for the Postgres.app as detailed here: https://github.com/econchick/new-coder/issues/142 – cpursley Jan 09 '15 at 11:41
  • How to specify this in the bundle file? – Colliot May 21 '15 at 11:58
  • 3
    The question is, why does this work? What does "ARCHFLAGS="-arch x86_64" do, and why doesn't the vanilla gem install work? It shouldn't be this tricky to install a gem. – Jared Menard Sep 15 '15 at 01:06
  • For this to work with bundler, add `export ARCHFLAGS="-arch x86_64"` to your `~/.bash_profile`. Open a new terminal window and run `bundle install`. Bundler doesn't have a configuration option for environmental variables so putting this in your `.bash_profile` will make it available to bundler when it goes to run `gem install`. – Avand Amiri Oct 22 '15 at 23:29
79

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.

Christine Loh
  • 801
  • 6
  • 5
31

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.

bobfet1
  • 1,603
  • 21
  • 22
  • 6
    x86_64 worked for me (Mac OS 10.6.8, PostgreSQL installed via homebrew: http://mxcl.github.com/homebrew/) – chesterbr Jul 22 '11 at 01:39
  • 2
    Have used hours to find a solution and yours worked, bobfet1 & @chester. Thanks! :-))) ** sudo env ARCHFLAGS="-arch x86_64" gem install pg ** – rassom Jul 28 '12 at 21:41
30

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.

eikes
  • 4,811
  • 2
  • 31
  • 31
25

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
etusm
  • 4,082
  • 1
  • 30
  • 26
25

Solution: reinstalled PostgreSQL with Homebrew.

Jason Swett
  • 43,526
  • 67
  • 220
  • 351
  • 1
    Congrats! Remember to award the bounty to someone. A few folks tried hard to help, even if it wasn't the final solution, and the reputation points were already spent when you offered the bounty. – justis Jun 15 '11 at 21:22
  • 2
    Not sure why this is got a -1, worked for me when non of the other methods did – Abe Petrillo Dec 26 '11 at 03:22
  • I also ended up having to use the ARCHFLAGS method, but it didn't work until after reinstalling postresql with homebrew. – Dave Cowart Nov 01 '13 at 21:29
  • Worked for Yosemite (10.10.3). brew install PostgreSQL – roosevelt Jun 05 '15 at 07:11
  • This didn't work for me. Wonder if it has something to do with the fact that I'm using the "built in" Ruby (I think) instead of anything else :| – rogerdpack Oct 13 '16 at 16:29
21

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
ybart
  • 876
  • 8
  • 23
14

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 as postgres
  • reinstall postgres to match the architecture in which the pg gem is built (by default it's the same as ruby'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:

which ruby
# You might need to use a different command if you use a ruby version manager
# "which ruby" if you use rvm
# "rbenv which ruby" if you use rbenv
# "asdf which ruby" if you use asdf
=> /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
13

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.

Abhi
  • 3,361
  • 2
  • 33
  • 38
6

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
Sean
  • 9,888
  • 4
  • 40
  • 43
  • I used the one-click installer instead of MacPorts for PostgreSQL. I don't know what the equivalent command would be. – Jason Swett Jun 06 '11 at 01:57
  • See if you get lucky with this (I think Mac's updatedb script runs on Saturday night): `locate pg_config` – Sean Jun 06 '11 at 04:55
  • I still don't know what that would mean for `CPPFLAGS` or `LDFLAGS` (I don't know what those are, sorry). – Jason Swett Jun 09 '11 at 01:28
  • Oh, but `locate pg_config` did show me some stuff, though. And I tried just installing PostgreSQL via MacPorts and using your command - didn't work. – Jason Swett Jun 09 '11 at 01:28
  • What was the error message? And did you add the path of `pg_config` to your `PATH`? – Sean Jun 09 '11 at 07:10
  • The output I get with your command is the same exact thing as what I originally got. And yes, I did add the path of `pg_config` to my `PATH`. – Jason Swett Jun 10 '11 at 15:53
  • 1
    Updated the above example. I believe that this will solve your problem. The catch was that `LDFLAGS` wasn't set to the directory that contained `libpq`. See above for details. – Sean Jun 10 '11 at 18:51
  • [This post](http://stackoverflow.com/questions/24627465/ruby-pg-gem-linking-to-wrong-copy-of-libpq-5-dylib-on-osx) may also be helpful. It addresses the same issue by simply putting a symlink to the correct file location in one of the directories already pointed to by LDFLAGS. – Jan Hettich Jul 08 '14 at 23:20
  • @JanHettich That works as a hack, but can lead to some very difficult to track down bugs in the event that the ABI is different. It's good that in that example, however, the symlink wasn't setup for "libpq.so" instead it was for the specific version. It's also very brittle, but in a one-off it works quite well and I can't count the number of times I've had to do that (e.g. linking against EnterpriseDB's copy of PostgreSQL). – Sean Aug 03 '14 at 16:47
5

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
Tibastral
  • 582
  • 5
  • 7
  • This worked in my case (fresh macOS High Sierra, using rbenv and installing mostly with brew). Bundle was likely using things from apple, that was the problem... – saza Oct 27 '17 at 02:43
5

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)

lnl
  • 358
  • 4
  • 11
3

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/

  • The above solution worked for me in Mac OS High Sierra sudo env ARCHFLAGS="-arch x86_64" gem install pg -v '1.1.2' -- with-pg-include=/usr/local/Cellar/postgresql/10.5/include Locate the exact path for the include folder and update that accordingly. – maniempire Nov 03 '18 at 14:07
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.

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
Brett Bender
  • 19,388
  • 2
  • 38
  • 46
  • Running `gem install pg` (along with your two previous commands) instead of `sudo gem install pg` still gives the exact same results. – Jason Swett Jun 10 '11 at 17:46
  • Are you on 32- or 64-bit architecture? Have you tried the above with the other architecture flag? Also did you change `/Library/PostgreSQL/...` to the correct location of wherever your copy of the postgres installation is? – Brett Bender Jun 10 '11 at 18:51
  • Looked into my notes about doing the commands I listed. Looks like I installed Postgres via `sudo port install postgresql83 postgresql83-server`, similar to these instructions: http://flux88.com/2010/06/installing-postgresql-for-rails-on-mac-os-x/. It seems the standalone installer is 32-bit, so you want to set your arch flags to 32-bit even if your machine is 64-bit. If you can't get it to work, I'd suggest uninstalling and reinstalling via MacPorts then attempting the instructions above. Hope that helps! – Brett Bender Jun 10 '11 at 18:55
3

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.

1

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.

Leopd
  • 41,333
  • 31
  • 129
  • 167
1

This is how I made it to work on Mavericks. Note: I already had installed postgresql 9.3 from homebrew.

  1. Update Xcode to 5.0 from App Store

  2. Install command line developer tools

    xcode-select --install

  3. Agree to Xcode license

    sudo xcodebuild -license

  4. Install gem

    ARCHFLAGS="-arch x86_64" gem install pg

Community
  • 1
  • 1
Michał Szajbe
  • 8,830
  • 3
  • 33
  • 39
1

So basically I did this ;-)

brew install postgres
Jackie Chan
  • 2,654
  • 6
  • 35
  • 70
1

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

babie
  • 1,219
  • 1
  • 10
  • 13
0

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

mchapman17
  • 53
  • 4
0

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!

d1jhoni1b
  • 7,497
  • 1
  • 51
  • 37
0

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)

Marcos Riveros
  • 534
  • 4
  • 10
0

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
sudo install
  • 148
  • 1
  • 1
  • 7