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

当前回答

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

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

其他回答

@温菲尔德说:

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/

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

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

asdf global postgres <version>
gem install pg

我在Linux (Pop_OS) 20.10上使用Ruby(和其他版本)的版本管理器(asdf),在尝试了100万种不同的方法对其进行排序后,包括上述所有内容

TLDR

gem install pg -v '1.2.3' -- --with-pg-config='/home/username/.asdf/installs/postgres/12.6/bin/pg_config'

找到正确版本的PG install,并指向bin目录中的pg_config

与ARCH标志一起使用。

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

这解决了您遇到的相同问题。