简体   繁体   English

Heroku上偶尔出现Postgres错误:无法将主机名“<pg URL>”转换为地址:名称或服务未知(PG ::错误)

[英]Occasional Postgres error on Heroku: could not translate host name “<pg URL>” to address: Name or service not known (PG::Error)

I'm using the shared postgres (dev / free) plan right now on Heroku and I see this error in my logs a decent amount. 我现在正在Heroku上使用共享postgres(dev / free)计划,我在日志中看到这个错误相当多。 Connecting to the PG database does usually work though, so I'm not sure if this is something that's within my control. 虽然连接到PG数据库通常都有效,但我不确定这是否属于我的控制之内。 The Heroku Status page does not list any downtime for the shared databases when these occur. Heroku状态页面不会列出发生这些情况时共享数据库的任何停机时间。

Using Rails 3.1.1 and the Unicorn web server . 使用Rails 3.1.1和Unicorn Web服务器

Error: could not translate host name "pg60.sharedpg.heroku.com ( http://pg60.sharedpg.heroku.com )" to address: Name or service not known (PG::Error) 错误:无法将主机名“pg60.sharedpg.heroku.com( http://pg60.sharedpg.heroku.com )”转换为地址:名称或服务未知(PG ::错误)

My Unicorn config file (after looking around at the recommended settings for Unicorn in Heroku): 我的Unicorn配置文件(在Heroku中查看Unicorn的推荐设置后):

worker_processes 3 # amount of unicorn workers to spin up
timeout 30         # restarts workers that hang for 30 seconds
preload_app true

before_fork do |server, worker|
  # Replace with MongoDB or whatever
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection_handler.clear_all_connections!
    ActiveRecord::Base.connection.disconnect!
    Rails.logger.info('Disconnected from ActiveRecord')
  end

  # If you are using Redis but not Resque, change this
  if defined?(Resque)
    Resque.redis.quit
    Rails.logger.info('Disconnected from Redis')
  end
end

after_fork do |server, worker|
  # Replace with MongoDB or whatever
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    ActiveRecord::Base.connection_handler.verify_active_connections!
    Rails.logger.info('Connected to ActiveRecord')
  end

  # If you are using Redis but not Resque, change this
  if defined?(Resque)
    Resque.redis = ENV['REDIS_URI']
    Rails.logger.info('Connected to Redis')
  end
end

Trace: 跟踪:

 328 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `initialize': could not translate host name "pg60.sharedpg.heroku.com (http://pg60.sharedpg.heroku.com)" to address: Name or service not known (PG::Error)
219 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `new'
223 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `connect'
225 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:301:in `initialize'
217 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
235 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'

328 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `initialize': could not translate host name "pg60.sharedpg.heroku.com (http://pg60.sharedpg.heroku.com)" to address: Name or service not known (PG::Error)
219 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `new'
223 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `connect'
225 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:301:in `initialize'
217 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
235 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
235 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:304:in `new_connection'
244 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:323:in `checkout_new_connection'
249 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block (2 levels) in checkout'
225 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `loop'
238 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `block in checkout'
153 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
229 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:260:in `checkout'
231 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:162:in `connection'
240 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
249 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
239 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
194 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:703:in `table_exists?'
128 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/app.rb:38:in `<top (required)>'
116 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from config.ru:9 (http://config.ru:9):in `require'
124 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from config.ru:9 (http://config.ru:9):in `block in <main>'
179 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
176 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
112 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from config.ru:1 (http://config.ru:1):in `new'
115 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from config.ru:1 (http://config.ru:1):in `<main>'
168 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn.rb:44:in `eval'
180 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn.rb:44:in `block in builder'
181 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:691:in `call'
187 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:691:in `build_app!'
182 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:136:in `start'
178 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/bin/unicorn:121:in `<top (required)>'
146 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/bin/unicorn:19:in `load'
148 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/bin/unicorn:19:in `<main>' 

That's a DNS error. 那是一个DNS错误。 It's exceedingly unlikely to be anything to do with your code. 它与你的代码几乎没有任何关系。 If both your Rails app and your DB are within Heroku, contact Heroku support. 如果您的Rails应用程序和数据库都在Heroku中,请联系Heroku支持。

If your Rails app is running outside the Heroku cloud (as a bit of searching for the term "Unicorn" suggests it could be; please link to things like this; not everyone knows what "unicorn" is ), you need to do some debugging to work out where the DNS issues are. 如果你的Rails应用程序在Heroku云之外运行(因为有点搜索“Unicorn”这个术语可能是这样; 请链接到这样的事情;不是每个人都知道“独角兽”是什么 ),你需要做一些调试找出DNS问题的位置。 Is it the recursive DNS server your app is using? 它是您的应用程序使用的递归DNS服务器吗? Some closer caching server? 一些更近的​​缓存服务器? Some intermediate server up the chain used to resolve Heroku's DNS (unlikely) ? 链上的一些中间服务器用于解析Heroku的DNS(不太可能)? Heroku's DNS its self? Heroku的DNS是自己的吗? Is it a packet loss issue, an issue where the server responds with a false negative, one bad server in a round-robin cluster, ... ? 这是一个数据包丢失问题,服务器响应假负,一个循环群集中的一个坏服务器,...? Expect to spend lots of quality time with dig +trace and with traceroute or mtr - assuming you can ssh in. If you have root, tcpdump and tshark may also be useful, as will examination of the logs of the DNS server you're using if you can get access to them. 期望用dig +tracetraceroutemtr花费大量的时间 - 假设你可以ssh 。如果你有root, tcpdumptshark也可能有用,检查你正在使用的DNS服务器的日志如果你可以访问它们。

If you have no shell access you pretty much have to bug the support of the people you're hosting with. 如果你没有shell访问权限,你几乎不得不破坏你所托管的人的支持。

Just for reference, I had the same problem (Heroku was having an issue with AWS). 仅供参考,我遇到了同样的问题(Heroku在AWS上遇到了问题)。 Here was the helpful response from customer service (08/15): 以下是客户服务(08/15)的有用回复:

We are occasionally seeing DNS resolution failure on our instances and we're working to solve this with AWS. 我们偶尔会在我们的实例上看到DNS解析失败,我们正在努力通过AWS解决这个问题。 The issue is really intermittent and unfortunately we're having hard time trying to identify the cause. 问题实际上是间歇性的,不幸的是我们很难找到原因。 While we're investigating, I have one workaround for this issue - currently, you're using Django's default way for the database connection - which is trying to make a new connection per request. 在我们正在调查时,我有一个解决此问题的方法 - 目前,您正在使用Django的默认方式进行数据库连接 - 它正在尝试为每个请求建立一个新连接。 This is pretty expensive, also this means you'll need to do DNS resolution for your every request which would increase the possibility to face this issue very unfortunately. 这非常昂贵,这也意味着您需要针对您的每个请求进行DNS解析,这将极大地增加面对此问题的可能性。 Instead, I recommend that you use connection pooling. 相反,我建议您使用连接池。 You can find some examples here: 你可以在这里找到一些例子:

https://devcenter.heroku.com/articles/python-concurrency-and-database-connections https://devcenter.heroku.com/articles/python-concurrency-and-database-connections

One customer who was seeing this issue occasionally (but frequently, with Django app) saw a good improvement with the connection pooling, this should help a lot to prevent it from happening. 一个偶尔会看到这个问题的客户(但经常使用Django应用程序)看到了连接池的良好改进,这应该有助于防止它发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 无法将主机名“postgres”转换为地址:名称或服务未知 (PG::ConnectionBad) - Rails could not translate host name “postgres” to address: Name or service not known (PG::ConnectionBad) 如何在 Docker 中运行 Rails? PG::ConnectionBad 无法将主机名“pg”转换为地址:没有与主机名关联的地址 - How do I run Rails in Docker? PG::ConnectionBad could not translate host name "pg" to address: No address associated with hostname gitlab ci 错误无法将主机名“postgres”转换为地址:名称无法解析 - gitlab ci error could not translate host name "postgres" to address: Name does not resolve ubuntu 19.04上的“无法将主机名“ db”转换为地址:名称或服务未知)(由于升级?) - “could not translate host name ”db“ to address: Name or service not known” on ubuntu 19.04 (due to upgrade?) 选择Heroku PG错误 - Heroku PG error on select PG :: UndefinedTable:错误:在Heroku上 - PG::UndefinedTable: ERROR: on heroku Heroku pg迁移错误“PG:DataCorrupted:Error” - Heroku pg migration error “PG:DataCorrupted: Error” 休斯顿的APN#push呼叫偶尔出现“ getaddrinfo:名称或服务未知”错误 - Occasional 'getaddrinfo: Name or service not known' error on Houston's APN#push call 部署到Heroku PG :: DuplicateColumn:错误: - Deploy to Heroku PG::DuplicateColumn: ERROR: PG ::错误:错误:关系“ table_name”不存在 - PG::Error: ERROR: relation “table_name” does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM