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

当前回答

在Mac上安装postgres然后捆绑安装

其他回答

我必须在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

你只要到这里看看你的pg版本是否支持Win32平台,然后使用这个命令来安装:

Gem install pg -v 0.14.1——platform=x86-mingw32

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

与ARCH标志一起使用。

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

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

Ubuntu 20.10 (pop!_os) Ruby 2.7.2 Rails 3.1.0 Postgresql 12

卸载后重新安装postgresql-client libpq5 libpq-dev

sudo apt remove postgresql-client libpq5 libpq-dev
sudo apt install postgresql-client libpq5 libpq-dev

然后再次安装pg gem,指向/usr/lib,找到pg库:

gem install pg  --   --with-pg-lib=/usr/lib

输出(你应该在前面的命令之后看到的):

Building native extensions with: '--with-pg-lib=/usr/lib'
This could take a while...
Successfully installed pg-1.2.3
Parsing documentation for pg-1.2.3
Installing ri documentation for pg-1.2.3
Done installing documentation for pg after 1 seconds
1 gem installed

Gem应该安装,然后继续正常的bundle安装或更新:

bundle
bundle install
bundle update