我尝试使用宝石安装pg,但它似乎不工作。

Gem install pg给出这个错误

Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

C:/Ruby/bin/ruby.exe extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
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
        --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=C:/Ruby/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


Gem files will remain installed in C:/Ruby/lib/ruby/gems/1.8/gems/pg-0.10.1 for
inspection.
Results logged to C:/Ruby/lib/ruby/gems/1.8/gems/pg-0.10.1/ext/gem_make.out

当前回答

问题是gem依赖,所以在安装pg之前,请确保您已经安装了“libpq-dev”

Ubuntu系统:

Sudo apt-get安装libpq-dev

RHEL 系统:

Yum安装postgresql-devel

麦克:

编译安装postgresql

其他回答

不管你运行的是什么操作系统,都要查看“Makefile”的日志文件,看看发生了什么,而不是盲目地安装东西。

在我的例子中,MAC OS,日志文件在这里:

/Users/za/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-1.0.0/mkmf.log

日志提示无法创建make文件,原因如下:

Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers

在mkmf.log中,您将看到它无法找到完成构建所需的库。

checking for pg_config... no
Can't find the 'libpq-fe.h header
blah blah

运行“brew install postgresql”后,我可以看到所有需要的库都在那里:

za:myapp za$ cat /Users/za/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-1.0.0/mkmf.log | grep yes
find_executable: checking for pg_config... -------------------- yes
find_header: checking for libpq-fe.h... -------------------- yes
find_header: checking for libpq/libpq-fs.h... -------------------- yes
find_header: checking for pg_config_manual.h... -------------------- yes
have_library: checking for PQconnectdb() in -lpq... -------------------- yes
have_func: checking for PQsetSingleRowMode()... -------------------- yes
have_func: checking for PQconninfo()... -------------------- yes
have_func: checking for PQsslAttribute()... -------------------- yes
have_func: checking for PQencryptPasswordConn()... -------------------- yes
have_const: checking for PG_DIAG_TABLE_NAME in libpq-fe.h... -------------------- yes
have_header: checking for unistd.h... -------------------- yes
have_header: checking for inttypes.h... -------------------- yes
checking for C99 variable length arrays... -------------------- yes

@温菲尔德说:

pg gem需要绑定postgresql客户端库。这个错误通常意味着它找不到你的Postgres库。要么你没有安装它们,要么你可能需要传递——with-pg-dir=到你的gem安装中。

不仅如此,您只需要——with-pg-config=来安装它。

在Mac上

如果你碰巧也通过mac上的website bundle安装了postgres,它应该在/Applications/ postgres .app/Contents/Versions/9.3/bin这样的地方。

所以,你要么在gem install中传递它:

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

或者正确地设置了PATH。因为这可能太多了,临时设置PATH:

export PATH=%PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin/

我必须在CentOS 5.8上这样做。运行捆绑包安装一直导致问题,因为我不能强制它使用特定的PG版本。

我也不能删除postgresql postgresql-devel,因为依赖关系的问题(它会删除php, http等)

解决方案?临时打乱$PATH以优先更新pgsql而不是默认pgsql:

export PATH=/usr/pgsql-9.2/bin:$PATH
bundle install

基本上,使用上面的命令,它会在/usr/bin/pg_config之前查看/usr/pgsql-9.2/bin/pg_config

回答: 不能在Windows上安装pg gem

的Windows原生版本 pg的最新版本(0.10.0)已经发布 昨天,但如果你安装了0.9.0它 是否应该安装二进制文件 问题。

Mac用户

PATH=$PATH:/Library/PostgreSQL/9.4/bin/ gem install pg

这应该能奏效