我使用的是Ruby on Rails 3.1预版本。我喜欢使用PostgreSQL,但问题是安装pg gem。它给了我以下错误:

$ gem install pg
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

        /home/u/.rvm/rubies/ruby-1.9.2-p0/bin/ruby 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=/home/u/.rvm/rubies/ruby-1.9.2-p0/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 /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
    Results logged to /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out

我怎么解决这个问题?


当前回答

找不到libpq-fe.h头文件

我在CentOS 7.0.1406上运行以下命令成功:

~ % psql --version # => psql (PostgreSQL) 9.4.1
yum install libpqxx-devel
gem install pg -- --with-pg-config=/usr/pgsql-9.4/bin/pg_config

或者,你可以配置捆绑器总是安装pg,使用这些选项(有助于在部署环境中运行捆绑器),

包配置构建。pg - with-pg-config = / usr / bin / pg_config / pgsql - 9.4

其他回答

对于任何基于debian的发行版(包括Ubuntu),一个更通用的答案如下。首先,以root身份安装apt-file包:

apt-get install apt-file

这允许您搜索包含文件的包。然后,更新其数据库使用

apt-file update

(这可以作为普通用户运行)。然后,使用以下方法查找丢失的头文件:

apt-file search libpq-fe.h

在我的机器上,这给出:

libpq-dev: /usr/include/postgresql/libpq-fe.h
postgres-xc-server-dev: /usr/include/postgres-xc/server/gtm/libpq-fe.h

好了!

在Mac上,我用下面的代码解决了这个问题:

gem install pg -v '0.18.4' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config

在MacOS M1上解决:

brew install libpq

然后我输入我的路径

echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

And:

gem install pg

我已经尝试了所有的解决方案,但只在命令下工作

pip install psycopg2-binary

在Ubuntu 20.04上,我已经有libpq5:amd64 version 13.2-1.pgdg20.04+1。现在,当我试图从apt存储库安装libpq-dev时,我正在接收

The following packages have unmet dependencies:
libpq-dev : Depends: libpq5 (= 12.6-0ubuntu0.20.04.1) but 13.2-1.pgdg20.04+1 is to be installed

我从http://ftp.us.debian.org/debian/pool/main/p/postgresql-13/libpq-dev_13.2-1_amd64.deb上获取了deb,并做了以下工作:

mkdir -p /tmp/libpq-dev
dpkg-deb -R libpq-dev_13.2-1_amd64.deb /tmp/libpq-dev
sed -i 's|= 13.2-1|= 13.2-1.pgdg20.04+1|' /tmp/libpq-dev/DEBIAN/control
dpkg-deb -b /tmp/libpq-dev /tmp/libpq-dev-new.deb
sudo dpkg -i /tmp/libpq-dev-new.deb

从那以后,我一直在愉快地使用宝石。没有额外的安装或任何东西。