远程连接到Heroku pg数据库错误:无法将主机名转换为地址

时间:2017-02-09 12:01:15

标签: ruby postgresql heroku activerecord database-connection

我正在尝试连接到我的Heroku托管postgres数据库但不幸的是它返回了一个"未能将主机名转换为地址"错误。我直接从heroku获取DATABASE_URL,如下面的establish_connection块所示

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)为什么它不能使用拆分方法

Tl;博士:Heroku的文档没有解释如何远程连接到应用程序的数据库,或者至少他们的文档暗示主机应该是完整的DATABASE_URL。

谢谢, SOliver

1 个答案:

答案 0 :(得分:0)

尝试使用GUI客户端(如PSequel)连接到服务器,以查看数据库是否确实存在。 如果不是,请执行

bundle exec rake db:create
bundle exec rake db:migrate