问题重现: 在bundle
的时候出现 gem 包pg-0.18.4
安装出错的情况,错误代码如下:
$ bundle
.
.
.
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/vagrant/.rvm/gems/ruby-2.3.0/gems/pg-0.18.4/ext
/home/vagrant/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20160415-7139-1cu08ba.rb 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/vagrant/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/home/vagrant/.rvm/gems/ruby-2.3.0/extensions/x86_64-linux/2.3.0/pg-0.18.4/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /home/vagrant/.rvm/gems/ruby-2.3.0/gems/pg-0.18.4 for inspection.
Results logged to /home/vagrant/.rvm/gems/ruby-2.3.0/extensions/x86_64-linux/2.3.0/pg-0.18.4/gem_make.out
.
.
.
An error occurred while installing pg (0.18.4), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.4'` succeeds before bundling.
解决方案: 先不要急着按提示去执行,出现这个问题可能是你没有安装 PostgreSQL 或是没有指定 pgsql 的路径。如果没有安装 PostgreSQL,点击传送门去安装 (注意:执行yum install postgresql-server
后继续往下看文档安装 pgsql 的扩展,建议最好安装的 PostgreSQL 是 9.X 以上版本,否则许多新特性无法使用)。英文不太好的同学可以到这篇博客PostgreSQL 新手入门看看。
步骤 1:
安装libpq-dev
包
Ubuntu 执行以下命令:
$ apt-get install libpq-dev
CentOS/RetH执行以下命令:
$ yum install postgresql-devel
步骤 2:
找到你的 pgsql 安装目录
我的是在/usr/pgsql-9.4
,也有可能会在/usr/local/pgsql
,因系统而异。
步骤 3:
with-pg-config
后面接的就是 pgsql 目录下的 pg_config 文件,注意--with-pg-config
前面还有两个-
$ gem install pg -v '0.18.4' -- --with-pg-config=/usr/pgsql-9.4/bin/pg_config
步骤 4:
重新执行bundle
命令
问题重现: 运行rails s -b 0.0.0.0 -p 3000
后,在浏览器打开项目首页出现下面问题
PG::ConnectionBad (FATAL: Ident authentication failed for user "postgres"
):
activerecord (4.2.3) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `initialize'
activerecord (4.2.3) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `new'
activerecord (4.2.3) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `connect'
activerecord (4.2.3) lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
activerecord-postgis-adapter (3.1.0) lib/active_record/connection_adapters/postgis_adapter.rb:51:in `initialize'
.
.
.
解决方案:
找到 pg_hba.conf
文件,一般是在/var/lib/pgsql/data
目录下,如果修改后不生效看看/var/lib/pgsql
目录下是否还有其他的数据目录。因系统环境而异。
使用vim
或vi
打开
步骤 1:
$ vim /var/lib/pgsql/data/pg_hba.conf
步骤 2:
按住shift
+ g
将光标定位的文件底部,按i
进入编辑模式,修改 METHOP 为 md5 验证。
完成后按shift
+ :
进入命令模式,输入wq
完成编辑。
下面给出修改后效果
.
.
.
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
步骤 3:
重启postgresql
服务
$ service postgresql restart
问题重现: 执行rake db:migrate
时出现错误,错误代码如下:
$ rake db:migrate
.
.
.
== 20151208044806 CreateShops: migrating ======================================
-- create_table(:shops)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedObject: ERROR: type "json" does not exist
LINE 1: ...ying NOT NULL, "logo" character varying, "images" json, "reg...
.
.
.
解决方案:出现这种问题大多是因为安装了老版的 PostgreSQL,在 CentOS 上面执行yum install postgresql
默认是 8.X 版本。升级版本即可。
步骤 1:删除旧版 postgresql
$ yum remove postgresql*
步骤 2:更新 yum
$ yum update
步骤 3: 到http://yum.pgrpms.org/reporpms/选择 9.X 以上版本下载相应的 rpm 包
$ wget https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-2.noarch.rpm
步骤 4:使用下载好的 rpm 包
$ rpm -ivh pgdg-centos94-9.4-2.noarch.rpm
步骤 5:安装postgresql94-server
yum -y install postgresql94-server
步骤 6:重新启动postgresql-94
服务
$ service postgresql-9.4 start
问题重现:执行rake db:migrate
时出现错误,错误代码如下:
$ rake db:migrate
.
.
.
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedObject: ERROR: type "geography" does not exist
LINE 1: ... "address" character varying NOT NULL, "location" geography(...
.
.
.
解决方案: 这是由于没有安装支持geography
类型数据的扩展,笔者使用的是 PostgreSQL-9.4 版本,这里给出 9.X 版本的解决方案。为了后续用到其他扩展方便,这里也就一起安装了。
步骤 1: list
命令查看postgresql
有哪些扩展,当你看到下面效果说明你的 yum 库中有这些扩展,如果没有请到http://yum.pgrpms.org/reporpms/选择 9.X 以上版本下载相应的 rpm 包安装。如果不需要请跳过步骤 1,步骤 2
$ yum list postgresql94-*
已加载插件:fastestmirror
Repository pgdg94 is listed more than once in the configuration
Repository pgdg94-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: mirrors.yun-idc.com
* extras: mirrors.yun-idc.com
* updates: mirrors.yun-idc.com
已安装的软件包
postgresql94.x86_64 9.4.7-1PGDG.rhel6 @pgdg94
postgresql94-libs.x86_64 9.4.7-1PGDG.rhel6 @pgdg94
postgresql94-server.x86_64 9.4.7-1PGDG.rhel6 @pgdg94
可安装的软件包
postgresql94-contrib.x86_64 9.4.7-1PGDG.rhel6 pgdg94
postgresql94-debuginfo.x86_64 9.4.7-1PGDG.rhel6 pgdg94
postgresql94-devel.x86_64 9.4.7-1PGDG.rhel6 pgdg94
postgresql94-docs.x86_64 9.4.7-1PGDG.rhel6 pgdg94
postgresql94-jdbc.x86_64 9.3.1101-1PGDG.rhel6 pgdg94
postgresql94-jdbc-debuginfo.x86_64 9.3.1101-1PGDG.rhel6 pgdg94
postgresql94-odbc.x86_64 09.03.0400-1PGDG.rhel6 pgdg94
postgresql94-odbc-debuginfo.x86_64 09.03.0400-1PGDG.rhel6 pgdg94
postgresql94-plperl.x86_64 9.4.7-1PGDG.rhel6 pgdg94
postgresql94-plpython.x86_64 9.4.7-1PGDG.rhel6 pgdg94
postgresql94-pltcl.x86_64 9.4.7-1PGDG.rhel6 pgdg94
postgresql94-python.x86_64 4.2-1PGDG.rhel6 pgdg94
postgresql94-python-debuginfo.x86_64 4.2-1PGDG.rhel6 pgdg94
postgresql94-tcl.x86_64 2.1.1-1.rhel6 pgdg94
postgresql94-tcl-debuginfo.x86_64 2.1.1-1.rhel6 pgdg94
postgresql94-test.x86_64
步骤 2:安装扩展
$ sudo yum install postgresql94-*
步骤 3: 前往postgis安装扩展。
友情提示:自行编译源码的话,如果系统编译环境不完全,会折腾很久,建议直接用 yum 安装。推荐一篇文章centos 安装 postgis
。
作者是为 PostgreSQL 源加上 EPEL 源,直接 yum 安装,无痛解决依赖问题。抓狂的同学速度 get。如果依然报错,请执行rake db:drop
,然后再创建一次数据库就行了。