简体   繁体   English

Rails 无法将主机名“postgres”转换为地址:名称或服务未知 (PG::ConnectionBad)

[英]Rails could not translate host name “postgres” to address: Name or service not known (PG::ConnectionBad)

I'm working on a Rails 4.2.3 project with ruby 2.2.2 (Running ubuntu 16.04).我正在使用 ruby​​ 2.2.2(运行 ubuntu 16.04)开发 Rails 4.2.3 项目。 It was working ok but after revisiting it after a while I can't serve.它工作正常,但在一段时间后重新访问后我无法服务。 I've tried searching but all situations don't seem to be of help.我试过搜索,但所有情况似乎都没有帮助。 Here's the error log:这是错误日志:

bigzoo@bigzoo:~/Documents/projects/proj$ rs
=> Booting Puma
=> Rails 4.2.3 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `initialize': could not translate host name "postgres" to address: Name or service not known (PG::ConnectionBad)
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `new'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `connect'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_handling.rb:87:in `connection'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/querying.rb:39:in `find_by_sql'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/relation.rb:638:in `exec_queries'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/relation.rb:514:in `load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/relation.rb:243:in `to_a'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/relation/delegation.rb:46:in `map'
    from /home/bigzoo/Documents/projects/proj/app/admin/product.rb:62:in `block in <top (required)>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/dsl.rb:15:in `instance_exec'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/dsl.rb:15:in `run_registration_block'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/namespace.rb:223:in `parse_registration_block'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/namespace.rb:48:in `register'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/application.rb:154:in `register'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin.rb:75:in `register'
    from /home/bigzoo/Documents/projects/proj/app/admin/product.rb:1:in `<top (required)>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/application.rb:212:in `block in load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/error.rb:41:in `capture'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/application.rb:212:in `load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/application.rb:204:in `block in load!'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/application.rb:204:in `each'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/application.rb:204:in `load!'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin/application.rb:226:in `routes'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/bundler/gems/activeadmin-21f76b1faf65/lib/active_admin.rb:79:in `routes'
    from /home/bigzoo/Documents/projects/proj/config/routes.rb:20:in `block in <top (required)>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:434:in `instance_exec'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:434:in `eval_block'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:412:in `draw'
    from /home/bigzoo/Documents/projects/proj/config/routes.rb:1:in `<top (required)>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application/routes_reloader.rb:40:in `each'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/file_update_checker.rb:75:in `call'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/file_update_checker.rb:75:in `execute'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/initializable.rb:30:in `run'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `each'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `call'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application.rb:352:in `initialize!'
    from /home/bigzoo/Documents/projects/proj/config/environment.rb:5:in `<top (required)>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /home/bigzoo/Documents/projects/proj/config.ru:3:in `block in <main>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/rack-1.6.5/lib/rack/builder.rb:55:in `instance_eval'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/rack-1.6.5/lib/rack/builder.rb:55:in `initialize'
    from /home/bigzoo/Documents/projects/proj/config.ru:in `new'
    from /home/bigzoo/Documents/projects/proj/config.ru:in `<main>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/rack-1.6.5/lib/rack/builder.rb:49:in `eval'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/rack-1.6.5/lib/rack/builder.rb:49:in `new_from_string'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/rack-1.6.5/lib/rack/builder.rb:40:in `parse_file'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/rack-1.6.5/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/rack-1.6.5/lib/rack/server.rb:208:in `app'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/server.rb:61:in `app'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/rack-1.6.5/lib/rack/server.rb:336:in `wrapped_app'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/server.rb:139:in `log_to_stdout'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/server.rb:78:in `start'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
    from /home/bigzoo/Documents/projects/proj/bin/rails:8:in `require'
    from /home/bigzoo/Documents/projects/proj/bin/rails:8:in `<top (required)>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/client/rails.rb:28:in `load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/client/rails.rb:28:in `call'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/client/command.rb:7:in `call'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/client.rb:26:in `run'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/spring-1.3.6/bin/spring:48:in `<top (required)>'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/binstub.rb:11:in `load'
    from /home/bigzoo/Documents/projects/proj/.bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/binstub.rb:11:in `<top (required)>'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/bigzoo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/bigzoo/Documents/projects/proj/bin/spring:13:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

The only issue seems to be connecting to postgres.唯一的问题似乎是连接到 postgres。

PS.附注。 I've already tried running unset DATABASE_URL if anyone is feeling compelled to suggest that.如果有人觉得有必要提出建议,我已经尝试运行unset DATABASE_URL

Extra info:额外信息:

bigzoo@bigzoo:~/Documents/projects/proj$ sudo netstat -atpn | grep 5432
[sudo] password for bigzoo: 
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      1744/postgres 

Adding the line below to /etc/hosts did the trick.将下面的行添加到 /etc/hosts 就可以了。

127.0.0.1 postgres

did the trick.成功了。

Rails uses the host name postgres and leaves it up to the system to direct postgres to the host and port as required. Rails 使用主机名 postgres 并让系统根据需要将 postgres 定向到主机和端口。 The line above allows the mapping to take place if it doesn't work for some reason.如果由于某种原因不起作用,上面的行允许进行映射。

Credits @mudasobwa.积分@mudasobwa。

PS.附注。 To edit /etc/hosts just type (in your console)要编辑 /etc/hosts 只需键入(在您的控制台中)

sudo nano /etc/hosts

To open it and add the line.打开它并添加该行。

This happened to me on a container that I had been using regularly.这发生在我经常使用的容器上。 It just randomly started throwing this error and not resolving.它只是随机开始抛出这个错误而不是解决。

I eventually solved it with我最终解决了它

docker system prune --volumes

running it without the volumes flag didn't resolve it.在没有卷标志的情况下运行它没有解决它。

if you run with docker add :如果您使用 docker add 运行:

        services:
        postgres:
          image: postgres
        environment:
          POSTGRES_HOST_AUTH_METHOD: trust

暂无
暂无

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

相关问题 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) 如何在 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 ubuntu 19.04上的“无法将主机名“ db”转换为地址:名称或服务未知)(由于升级?) - “could not translate host name ”db“ to address: Name or service not known” on ubuntu 19.04 (due to upgrade?) gitlab ci 错误无法将主机名“postgres”转换为地址:名称无法解析 - gitlab ci error could not translate host name "postgres" to address: Name does not resolve Rails-Postgres-无法连接到服务器:连接被拒绝(PG :: ConnectionBad) - Rails - Postgres - could not connect to server: Connection refused (PG::ConnectionBad) Rails在Mac OS X / PG :: ConnectionBad上找不到我的Postgres服务器:无法连接到服务器 - Rails cannot find my Postgres server on Mac OS X / PG::ConnectionBad: could not connect to server 名称解析暂时失败(PG :: ConnectionBad) - Temporary failure in name resolution (PG::ConnectionBad) PG :: ConnectionBad与rails和pgbouncer - PG::ConnectionBad with rails and pgbouncer Ruby on Rails PG :: ConnectionBad - Ruby on Rails PG::ConnectionBad Rails 3机械化-SocketError:getaddrinfo:主机或名称未知 - Rails 3 Mechanize - SocketError: getaddrinfo: Host or name not known
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM