找不到PostgreSQL客户端库(libpq)

我试图在Mac OS X 10.6上安装PostgreSQL for Rails。 首先,我尝试了MacPorts安装,但是这样做并不顺利,所以我做了一次点击DMG安装。 这似乎工作。

我怀疑我需要安装PostgreSQL开发包,但我不知道如何在OS X上做到这一点。

下面是我尝试做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 
 $ 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... 

成功了!

我在这里尝试了最高评分的答案:

 env ARCHFLAGS="-arch x86_64" gem install pg 

但是,当我尝试再次运行捆绑安装,它有相同的错误。 然后我试着用ARCHFLAGS来安装整个软件包,如下所示:

 ARCHFLAGS="-arch x86_64" bundle install 

为我工作! 请确保使用i386replacex86_64,具体取决于您拥有的体系结构。

我在使用EnterpiseDB .dmg时遇到了这个问题。 如果您认为您使用的是相同的,则可以通过指定正确的体系结构来实现它:

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

网上有一些教程指出一个不同的架构(比如使用MacPorts的人使用“-arch x86_64”),但是因为我使用了单个文件安装,所以它不适用于我。

也许你可以试试这个:

 ARCHFLAGS="-arch i386 -arch x86_64" gem install pg 

要知道你的图书馆的架构,你可以使用

 file /usr/local/lib/libpq.dylib 

在我的情况下只有1架构(通过自制软件安装):

 /usr/local/lib/libpq.dylib: Mach-O 64-bit dynamically linked shared library x86_64 

解决scheme:用Homebrew重新安装PostgreSQL。

如果使用优胜美地:

 brew install postgres 

然后:

 ARCHFLAGS="-arch x86_64" gem install pg 

(可选)最后,如果你想启动autovacuum …

 postgres -D /usr/local/var/postgres 

通过在适当的环境variables前添加虚假的gem 。 如果您是从MacPorts安装,则应该可以通过以下步骤进行操作:

 % /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 

从那里,拉出LIBDIRINCLUDEDIRCPPFLAGSLIBSLDFLAGS (然而我认为可以让你运行的是LIBDIR )。 然后你运行:

 setenv PATH /opt/local/lib/postgresql91/bin:${PATH} sudo env LDFLAGS=-L`pg_config --libdir` CPPFLAGS=`pg_config --cppflags` gem install pg 

这应该为你做。 让我知道如果没有。

我们遇到的问题很奇怪。

 ruby -v # was ok (rbenv) gem -v # was ok (rbenv) 

但是当我们做了一个bundle安装的时候,bundler并没有安装在rbenv安装的ruby版本中,所以当我们键入bundle install时,它使用了系统的bundler。

因此,在运行软件包安装之前,请确保您已经通过运行安装了捆绑软件

 gem install bundler 

我不认为你需要postgres的开发文件,你需要的一切应该已经包括在你的安装程序。 更有可能的是,它们安装的path不在你的环境path中,因此当它试图编译pg时,gem找不到它们。

你不应该以root身份运行gem install pg ,实际上如果你这样做的话,你的PATH(root的PATH,如果用w / sudo运行的话)将不包含必要的信息。

以下通常适用于我:

 # 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 

这是我最后做的(与其他职位一起提供的多个解决scheme的组合):

$ sudo env ARCHFLAGS =“ – arch x86_64”gem install pg – with-pg-include = / Library / PostgreSQL / 9.6 / include /

其他人提出的ARCHFLAGS答案如果以某种方式结束了64位版本的postgres(将自行安装)和32位版本的ruby,将无法正常工作。 出于某种原因, rbenv坚持要为我构buildruby 1.9.2-p290为32位,这使得无法链接到64位postgres。

检查您的ruby二进制文件的体系结构

 file `which ruby` 

或者如果使用rbenv

 file `rbenv which ruby` 

并比较你的postgres:

 file `which postgres` 

如果出现错误,您需要重新安装postgres或ruby。 用rbenv我解决了这个只是切换到不同的版本: 1.9.3-p194而不是1.9.2-p290

这就是我在小牛身上所做的。 注意:我已经从homebrew安装了postgresql 9.3。

  1. 从App Store更新Xcode到5.0

  2. 安装命令行开发者工具

    xcode-select –install

  3. 同意Xcode许可证

    sudo xcodebuild -license

  4. 安装gem

    ARCHFLAGS =“ – arch x86_64”gem install pg

所以基本上我做到了这一点;-)

 brew install postgres 

我可能晚了一点,但在我的情况下,我使用rbenv和升级到Ruby 2.2.3。 我不得不安装Bundler来让我的工作,我有一个旧的系统版本。

gem install bundler