无法使用 Postgre.app 在 Mavericks 中安装 pg gem

我正在尝试安装 pg gem,以便在我的本地机器上与 Postgre.app 一起使用,我正在运行 Mavericks。

App 已经安装并运行良好,但是我无法让 gem 工作:

  1. 使用 Postgre.app 文档中的命令‘ env ARCHFLAGS =”-arch x86 _ 64”gem install pg —— with-pg-config =/Applications/Postgre.app/content/MacOS/bin/pg _ config’
  2. 更新了自制软件并安装了 Apple GCC 4.2
  3. 安装了 Xcode 开发人员工具
  4. 更新了我的 $PATH 以引用 Postgre.app bin 和 lib 目录

所有这些都没有成功。下面是我收到的具体错误消息:

Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config'
This could take a while...
ERROR:  Error installing pg:
ERROR: Failed to build gem native extension.


/Users/Brian/.rvm/rubies/ruby-2.0.0-p353/bin/ruby extconf.rb --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Using config values from /Applications/Postgres.app/Contents/MacOS/bin/pg_config
sh: /Applications/Postgres.app/Contents/MacOS/bin/pg_config: No such file or directory
sh: /Applications/Postgres.app/Contents/MacOS/bin/pg_config: No such file or directory
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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
--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=/Users/Brian/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
--with-pg
--without-pg
--with-pg-config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/

如果您能提供任何帮助,我将不胜感激。谢谢!

35370 次浏览

You probably have the wrong path for --with-pg-config, check if it's actually there.

You can find the correct path to pg_config with:

find /Applications -name pg_config

In the latest Postgres.app Version the path is:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config

To resolve this issue I installed postgres using homebrew using the following in the terminal window:

brew install postgres

Homebrew can be found here

http://brew.sh

Being on a brand new mac, here's what I had to do:

  1. Install Xcode tools from the app store
  2. Open Xcode tools and accept the license
  3. Now run (hopefully a future-proofed command):

    version=$(ls /Applications/Postgres.app/Contents/Versions/ | tail -1) gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/$version/bin/pg_config .

If you have trouble, you can troubleshoot a bit by checking out the actual error in the mkmf.log which you can find by running (if using rvm):

cd ~/.rvm ; find . -name mkmf.log | grep pg

In my case (running Postgres.app v9.3.4.2) it only seemed to work when prepending the environment architecture flags:

env ARCHFLAGS="-arch x86_64" gem install pg -- \
--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

I was able to install pg with this command

    gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config

I found my path by running

    sudo find / -name "pg_config"

and I Successfully installed pg-0.17.1

This worked for me:

gem install pg -- --with-pg-config=`which pg_config`

adding the postgress bin dir to the path also does the trick. just add the bin to the pat like this. with recent installs the latest symbolic link makes sure that this path should be 'stable' for future version upgrades.

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin

For future reference, since a lot of us are posting new paths for the new version numbers:

Currently I'm seeing a symlink called latest in /Applications/Postgres.app/Contents/Versions/.

You should be able to just do:

$ gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config

And forget about the version number. This might not apply to every (older) version, but I myself was looking for a snippet I could save and reuse.