我尝试使用宝石安装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 install pg -- --with-pg-config=/usr/pgsql-9.1/bin/pg_config

其他回答

Gem install pg -- --with-pg-config=/usr/pgsql-9.1/bin/pg_config

@温菲尔德说:

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/
$ PATH=$PATH:/Library/PostgreSQL/9.1/bin sudo gem install pg

将9.1版本替换为系统上安装的版本。

我在Linux Mint (Maya) 13上解决了这个问题,我通过安装postgresql和postgresql-server来修复它:

apt-get install postgresql-9.1 

sudo apt-get install postgresql-server-dev-9.1

Mac用户

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

这应该能奏效