No pg_config...
問題重現(xiàn): 在bundle
的時候出現(xiàn)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.
解決方案: 先不要急著按提示去執(zhí)行,出現(xiàn)這個問題可能是你沒有安裝PostgreSQL或是沒有指定pgsql的路徑塞帐。如果沒有安裝PostgreSQL,點擊傳送門去安裝(注意:執(zhí)行yum install postgresql-server
后繼續(xù)往下看文檔安裝pgsql的擴展巍沙,建議最好安裝的PostgreSQL是9.X以上版本葵姥,否則許多新特性無法使用)。英文不太好的同學(xué)可以到這篇博客PostgreSQL新手入門看看句携。
步驟1:
安裝libpq-dev
包
Ubuntu執(zhí)行以下命令:
$ apt-get install libpq-dev
CentOS/RetH執(zhí)行以下命令:
$ yum install postgresql-devel
步驟2:
找到你的pgsql安裝目錄
我的是在/usr/pgsql-9.4
榔幸,也有可能會在/usr/local/pgsql
,因系統(tǒng)而異矮嫉。
步驟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:
重新執(zhí)行bundle
命令
無法連接pgsql
問題重現(xiàn): 運行rails s -b 0.0.0.0 -p 3000
后,在瀏覽器打開項目首頁出現(xiàn)下面問題
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
目錄下是否還有其他的數(shù)據(jù)目錄拨齐。因系統(tǒng)環(huán)境而異。
使用vim
或vi
打開
步驟1:
$ vim /var/lib/pgsql/data/pg_hba.conf
步驟2:
按住shift
+ g
將光標(biāo)定位的文件底部昨寞,按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
服務(wù)
$ service postgresql restart
type "json" does not exist
問題重現(xiàn): 執(zhí)行rake db:migrate
時出現(xiàn)錯誤掏导,錯誤代碼如下:
$ 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...
.
.
.
解決方案:出現(xiàn)這種問題大多是因為安裝了老版的PostgreSQL,在CentOS上面執(zhí)行yum install postgresql
默認是8.X版本羽峰。升級版本即可趟咆。
步驟1:刪除舊版postgresql
$ yum remove postgresql*
步驟2:更新yum
$ yum update
步驟3: 到http://yum.pgrpms.org/reporpms/選擇9.X以上版本下載相應(yīng)的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
服務(wù)
$ service postgresql-9.4 start
type "geography" does not exist
問題重現(xiàn):執(zhí)行rake db:migrate
時出現(xiàn)錯誤,錯誤代碼如下:
$ 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
類型數(shù)據(jù)的擴展限寞,筆者使用的是PostgreSQL-9.4版本忍啸,這里給出9.X版本的解決方案。為了后續(xù)用到其他擴展方便履植,這里也就一起安裝了计雌。
步驟1: list
命令查看postgresql
有哪些擴展,當(dāng)你看到下面效果說明你的yum庫中有這些擴展玫霎,如果沒有請到http://yum.pgrpms.org/reporpms/選擇9.X以上版本下載相應(yīng)的rpm包安裝凿滤。如果不需要請?zhí)^步驟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安裝擴展庶近。
友情提示:自行編譯源碼的話翁脆,如果系統(tǒng)編譯環(huán)境不完全,會折騰很久鼻种,建議直接用yum安裝反番。推薦一篇文章centos安裝postgis
。
作者是為PostgreSQL源加上EPEL源叉钥,直接yum安裝罢缸,無痛解決依賴問題。抓狂的同學(xué)速度get投队。如果依然報錯枫疆,請執(zhí)行rake db:drop
,然后再創(chuàng)建一次數(shù)據(jù)庫就行了敷鸦。