我尝试使用宝石安装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

其他回答

在macOS (El Capitan)上。你可以简单使用:brew install postgresql

我没有安装postgresql,所以我只是使用

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

Ubuntu 12.04。

这就解决了问题。


更新:

使用最新版本:

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

如果你使用asdf来安装postgres,只需要像这样安装pg gem:

asdf global postgres <version>
gem install pg

这对我来说很管用:

sudo apt-get install libpq-dev

我使用:

Ubuntu 14.04.2 LTS 2.2.2红宝石 Rails 4.2.1

$ PATH=$PATH:/Library/PostgreSQL/9.1/bin sudo gem install pg

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