PostgreSQL的gem包pg无法安装

31

我的数据库是PostgreSQL。我在CENTOS系统上... 在安装pg gem时,我收到了以下错误。我重新安装以清除我的YAML问题,但它只是一个警告。我的主要问题是安装pg gem:

[root@vdimc04 ~]# gem install pg
/usr/local/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

        /usr/local/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=/usr/local/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 /usr/local/lib/ruby/gems/1.9.1/gems/pg-0.14.1 for inspection.
Results logged to /usr/local/lib/ruby/gems/1.9.1/gems/pg-0.14.1/ext/gem_make.out

我也尝试了下面这个命令,但没有用:
 gem install pg -- --with-pg-config=/usr/pgsql-9.1/bin/pg_config

1
你应该找到并修复YAML问题。YAML用于处理配置文件,对Ruby非常有用。 - the Tin Man
我已经从链接http://pyyaml.org/download/libyaml/yaml-0.1.4 tar.gz安装了YAML。 - Pavan Kumar
你能说一下卸载 Ruby 的命令吗?我使用了 rpm -e ruby1.9.3-p327 命令进行卸载,但是在卸载后,如果我输入 'ruby -v' 命令,它会显示版本名称。这是否意味着它已经成功卸载了,我不知道... - Pavan Kumar
你是使用RVM还是通过yum安装的Ruby?如果是RVM,我手头有完整的CentOS RVM、Ruby、Rails、PostgreSQL配方。 - Alistair A. Israel
我通过下载ruby1.9.3-p327软件包并通过./configure,make,make install命令安装了Ruby。就这样。但是当我通过rpm -e ruby1.9.3p327卸载时,它显示已卸载,但如果我在卸载后键入ruby -v,则会显示版本名称... - Pavan Kumar
显示剩余2条评论
10个回答

85

我在Centos上遇到了同样的错误。通过谷歌搜索,我找到了两个命令:

  1. yum install postgresql-libs -- 它给我一个消息说,它已经安装了。
  2. yum install postgresql-devel -- 这解决了我安装'pg'.gem的相关错误。

希望能对你有所帮助 :)

apos 写道: 在Ubuntu 14.04上安装:

  1. apt-get install postgresql-server-dev-9.3 libpq-dev

我没有足够的声望来点赞...但在确保之后我会的。 我有一个疑问...我已经将database.yml编辑为postgres... 但是当我执行'rails server'时,它显示ActiveRecord :: ConnectionNotEstablished 如何解决这个问题... - Pavan Kumar
仅更改适配器名称是不够的,不同数据库的DB驱动程序配置也不同。您需要根据数据库更改所有属性。 - Prem
开发: 适配器:postgresql 数据库:数据库名称/development.postgresql 连接池:5 用户名:db_test_user 密码:password 主机:192.168.1.101测试: 适配器:postgresql 数据库:数据库名称 连接池:5 用户名:db_test_user 密码:password生产: 适配器:postgresql 数据库:production.postgresql 连接池:5 用户名:db_test_user 密码:password上述的database.yml文件是否正确,我不知道。请检查一下。 - Pavan Kumar
我已经为Ruby安装了'gem pg'。 - Pavan Kumar
使用 AWS Cloud 9 Linux:sudo yum install postgresql-libs然后:sudo yum install postgresql-devel然后:bundle install - kai_onthereal

20

这是在CentOS上安装PostgreSQL、RVM、Ruby和Rails的步骤:

PostgreSQL

首先,按照http://wiki.postgresql.org/wiki/YUM_Installation上的说明安装PostgreSQL:

sudo vi /etc/yum.repos.d/CentOS-Base.repo

[base]
exclude=postgresql*

[updates]
exclude=postgresql*
获取最新的PGDG RPM文件(针对您的平台/架构),例如:
curl -O http://yum.postgresql.org/9.2/redhat/rhel-latest-x86_64/pgdg-redhat92-9.2-7.noarch.rpm
那么
sudo rpm -ivh pgdg-redhat92-9.2-7.noarch.rpm
sudo yum install -y postgresql92-server postgresql92-devel

初始化数据库:

sudo service postgresql-9.2 initdb

根据需要配置/var/lib/pgsql/9.2/data/postgresql.conf/var/lib/pgsql/9.2/data/pg_hba.conf

启动PostgreSQL服务器:

sudo service postgresql-9.2 start

RVM

sudo yum -y groupinstall "Development Tools"
sudo yum -y install zlib zlib-devel sqlite-devel httpd curl-devel httpd-devel apr-devel apr-util-devel mlocate man libxml2-devel libxslt-devel libffi-devel readline-devel

请注意,不要以 root 权限运行:

\curl -L get.rvm.io | sudo bash -s stable

注销并重新登陆。然后,which rvm 应该回复 /usr/local/rvm/bin/rvmtype rvm|head -1 应该显示 rvm 是一个函数

Ruby + Rails

rvm install 1.9.3 --with-openssl-dir=/usr
rvm use 1.9.3 --default
gem install rake
gem install rails

PostgreSQL宝石

gem install pg -- --with-pg-config=/usr/pgsql-9.2/bin/pg_config

--with-pg-config 对于我最新安装的 CentOS 版本来说是必需的,否则 gem install pg 会找不到 pg_config。可能会因情况而异,但如果你按照上述所有内容严格执行(我在过去几个月中已经做过足够多次),如果有什么问题请让我知道。


你能告诉我如何启动Rails程序吗?由于我不熟悉这个,不知道如何连接程序到数据库。就像在Java中我们有jar文件一样,但是在这里我不知道如何连接数据库,以及在哪个文件夹中开始编写代码...... - Pavan Kumar
@Alistairsrael,谢谢你的建议啊..现在遇到了新问题: 我已经安装了Postgres库和pg gem,但是它无法连接到Postgres数据库。收到的错误信息为:ACTIVE RECORD: CONNECTION NOT ESTABLISHED。 - Pavan Kumar
不行,除非你想让服务器绑定到小于1024的端口。 - Alistair A. Israel
@Alistairlsrael,好的,谢谢你,非常感谢。我的服务器正常工作了。我对pg_hba.conf文件进行了一些更改。现在它可以正常运行了。 - Pavan Kumar
这是一个非常好的资源,可以帮助我们解决痛苦的流程!谢谢。 - scaryguy
显示剩余2条评论

6

请尝试执行以下命令安装libpq包:

sudo apt-get install libpq-dev

那个包可以帮助使用pg gem,使用后应该就没问题了。 :)

1
谢谢您的回复。但是我正在使用 CentOS,因此 sudo apt-get 是无法运行的。 - Pavan Kumar

5

试试这个,答案不是我写的,但对我有用。记得指向你的Postgres.app应用程序所在的位置:

bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config

那么执行:
bundle update
bundle install

3
$ sudo apt-get install libpq-dev
$ sudo apt-get install libdbd-pg-ruby1.8

然后:

$ sudo apt-get install pg  

针对CentOS系统,请查看下面的链接。

http://wiki.postgresql.org/wiki/YUM_Installation

安装pg到CentOS系统中。

run this commands

bundle config build.pg 

gem install pg -v '0.12.0'

谢谢您的回复。但是我使用的是CentOS系统,因此无法使用sudo apt-get命令。 - Pavan Kumar
嗨,我又更新了安装pg gem的答案。请尝试这个。 - vijikumar
bundle config build.pg --with-pg-config=/usr/pgsql-9.1/bin/pg_config - vijikumar
尝试使用以下命令:"bundle config build.pg --with-pg-config=/opt/local/lib/postgresql91/bin/pg_config"。我认为这会起作用,我们必须正确地指定路径。 - vijikumar
输入以下命令,它将显示文件的路径。 locate libpq-fe.h - vijikumar
显示剩余13条评论

1
尝试安装PostgreSQL服务器和相应的库。 然后安装pg gem。然后它将为您工作。

1
服务器已经安装好了... 那么对应的库是什么意思?你能给出任何要执行的命令吗? - Pavan Kumar
我已经安装了PostgreSQL的库和pg gem软件包,但是现在的问题是无法连接到PostgreSQL数据库。收到的错误为Active Record: Connection not established。 - Pavan Kumar

0

首先安装

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

接下来安装

sudo apt-get install libpq-dev

然后安装 gem install pg


谢谢您的回复。我正在使用CentOS系统,所以无法使用apt-get命令。 - Pavan Kumar
以root用户身份登录,然后使用“yum”代替“sudo apt-get”。 - Gowri Naidu R
只需查看此链接以安装“yum”和“postgresql”http://wiki.postgresql.org/wiki/Detailed_installation_guides,然后尝试gem install pg..希望对您有用。 - Gowri Naidu R

0
我的问题是在pg gem已经安装之前..但它没有加载到bundle中.. 现在的解决方案是: 当我们像'rails new Demo'这样声明一个新应用程序时,我们必须包括'--database=postgresql'. 所以现在总命令是:'rails new Demo --database=postgresql'
然后pg gem文件将被存储到bundle中..
在bundle中只有一个gem会被存储. 要么是pg gem或sqlite3 gem..
现在相应地编辑database.yml文件.. 就这样

0

使用 AWS Cloud 9 Linux:

sudo yum install postgresql-libs

然后:

sudo yum install postgresql-devel

然后:

gem update --system

如果需要,然后:

gem install bundler(Bundler 2.0)

然后:

bundle install


0

我遇到了同样的问题,尝试安装所有依赖项,但都无济于事。最后,我尝试使用PostgreSQL9.3而不是PostgreSQL9.4,一切都正常了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接