Ruby-on-rails 找不到 PostgreSQL 客户端库 (libpq)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6209797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:17:46  来源:igfitidea点击:

Can't find the PostgreSQL client library (libpq)

ruby-on-railsruby-on-rails-3postgresqlrubygems

提问by Jason Swett

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.

我正在尝试在 Mac OS X 10.6 上安装 PostgreSQL for Rails。首先,我尝试了 MacPorts 安装,但效果不佳,因此我进行了一键式 DMG 安装。那似乎奏效了。

I suspect I need to install the PostgreSQL development packages but I have no idea how to do that on OS X.

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

Here's what I get when I try to do sudo gem install pg:

这是我尝试做的事情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

采纳答案by Jason Swett

Solution: reinstalled PostgreSQL with Homebrew.

解决方案:用 Homebrew 重新安装 PostgreSQL。

回答by Siddhartha Mukherjee

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

成功了!

回答by Christine Loh

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:

但是当我再次尝试运行 bundle install 时,它出现了同样的错误。然后我尝试使用 ARCHFLAGS 安装整个包,如下所示:

ARCHFLAGS="-arch x86_64" bundle install

Worked for me! Make sure to replace x86_64 with i386 depending on what architecture you have.

对我来说有效!确保根据您拥有的架构将 x86_64 替换为 i386。

回答by bobfet1

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:

我在使用 EnterpiseDB .dmg 时遇到了这个问题。如果这与您使用的想法相同,那么我通过指定正确的架构来使其工作:

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.

网上有一些教程说要指定不同的体系结构(例如为使用 MacPorts 的人指定“-arch x86_64”),但它对我不起作用,因为我使用了单个文件安装。

回答by etusm

If using Yosemite:

如果使用优胜美地:

brew install postgres

Then:

然后:

ARCHFLAGS="-arch x86_64" gem install pg

And (optional)finally, if you want to launch autovacuum...

并且(可选)最后,如果您想启动 autovacuum ......

postgres -D /usr/local/var/postgres

回答by ybart

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):

在我的情况下,它只给出了 1 个架构(通过自制软件安装):

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

回答by Sean

Fake out gemby prefixing the appropriate environment variables. If you were installing from MacPorts, you should be able to walk through the following procedure:

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

From there, pull out the LIBDIR, INCLUDEDIR, CPPFLAGS, LIBSand LDFLAGS(the one that I think will get you running is LIBDIR, however). Then you'd run:

从那里,拉出LIBDIR, INCLUDEDIR, CPPFLAGS,LIBSLDFLAGS(但是,我认为会让你跑起来的那个是LIBDIR)。然后你会运行:

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.

那应该为你做。如果没有,请告诉我。

回答by Tibastral

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.

但是当我们实际上进行bundle install时,rbenv安装的ruby版本没有安装bundler,所以,当我们输入bundle install时,它使用了系统的bundler。

So before running bundle install, be sure that you have installed bundler by running

所以在运行 bundle install 之前,请确保您已经通过运行安装了 bundler

gem install bundler

回答by Marcus Silveira

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/

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

回答by Brett Bender

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.

我认为您不需要 postgres 开发文件,您需要的一切都应该包含在您的安装程序中。它们安装的路径更有可能不在您的环境路径中,因此 gem 在尝试编译 pg 时找不到它们。

You shouldn't have to run gem install pgas root, in fact if you do it's likely your PATH (root's PATH if run w/ sudo) won't contain the necessary info.

您不必以gem install pgroot身份运行,事实上,如果您这样做,很可能您的 PATH(如果使用 sudo 运行,则为 root 的 PATH)将不包含必要的信息。

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