【发布时间】:2017-02-09 12:01:14
【问题描述】:
我正在尝试连接到我的 Heroku 托管的 postgres 数据库,但不幸的是它返回了“无法将主机名转换为地址”错误。我直接从 heroku 获取 DATABASE_URL,如下面的建立连接块所示
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => `heroku config:get DATABASE_URL -amy_app`,
:database => "my_db",
:username => "postgres",
:password => "password")
错误的痕迹是:
C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': could not
translate host name "postgres://address_to_db.1.amazonaws.com:5432/ (PG::Connecti
onBad)
" to address: Unknown server error
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initializ
e'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql
_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new
_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `che
ckout_new_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acq
uire_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `blo
ck in checkout'
from C:/Ruby21-x64/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `che
ckout'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `blo
ck in connection'
from C:/Ruby21-x64/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `con
nection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `ret
rieve_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_handling.rb:87:in `connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/attributes.rb:93:in `columns'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/attributes.rb:98:in `columns_hash'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:64:in `block in valid?'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:64:in `each'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:64:in `all?'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:64:in `valid?'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
经过调查,我找到了解决问题的方法
db_parts = ENV['DATABASE_URL'].split(/\/|:|@/)
username = db_parts[3]
password = db_parts[4]
host = db_parts[5]
db = db_parts[7]
来自:Can't connect to PostgreSQL database on Heroku using Ruby - could not translate host name
当我这样做时,我会收到 NoDatabaseError。我手动输入了数据库名称,但没有成功。我不清楚这是否意味着它设法找到了数据库。
有人知道如何解决这个问题吗?任何帮助将不胜感激。
编辑:我无法通过运行heroku config:get DATABASE_URL -amy_app 并手动将预网址“连接信息”读取到各个字段来连接。目前尚不清楚为什么 a) 通过 ruby 系统调用发出请求时只返回 url。 b) 为什么使用 split 方法不起作用
Tl;dr:Heroku 的文档没有解释如何远程连接到应用程序的数据库,或者至少他们的文档暗示主机应该是完整的 DATABASE_URL。
谢谢, 索利弗
【问题讨论】:
标签: ruby postgresql heroku activerecord database-connection