【问题标题】:Remote connect to Heroku pg database error: could not translate host name to address远程连接到 Heroku pg 数据库错误:无法将主机名转换为地址
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。我可以使用heroku pg:psql 连接到数据库,并且应用程序本身连接到数据库。
    • 所以我想你做了一个“heroku run rake db:migrate”。你能再做一次,看看输出是什么?
    • 没有等待迁移,我几天前运行了几次,它们都处理正确。我已经手动拆分了 heroku config:get DATABASE_URL 的“连接信息”部分,它现在似乎正在连接。 Heroku 的文档没有讨论这个问题,所以我会通过电子邮件发送给他们,希望他们可以告诉用户他们需要拆分 URL。
    • 祝你好运
    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2017-03-02
    相关资源
    最近更新 更多