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

时间:2021-05-20 23:39:22

I'm working on a Rails 4.2.3 project with ruby 2.2.2 (Running ubuntu 16.04). 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:

我正在使用ruby 2.2.2(运行ubuntu 16.04)开发Rails 4.2.3项目。它工作正常,但经过一段时间重新访问后,我无法服务。我尝试过搜索,但所有情况似乎都没有帮助。这是错误日志:

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.

PS。我已经尝试过运行未设置的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 

1 个解决方案

#1


1  

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. The line above allows the mapping to take place if it doesn't work for some reason.

Rails使用主机名postgres并将其留给系统,以根据需要将postgres引导到主机和端口。如果由于某种原因不起作用,则上面的行允许映射。

Credits @mudasobwa.

积分@mudasobwa。

PS. If your new and don't know where /etc/hosts is just type (in bash)

PS。如果你的新的并且不知道/ etc / hosts只是在哪里键入(在bash中)

sudo nano /etc/hosts

To open it and add the line.

打开它并添加该行。

#1


1  

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. The line above allows the mapping to take place if it doesn't work for some reason.

Rails使用主机名postgres并将其留给系统,以根据需要将postgres引导到主机和端口。如果由于某种原因不起作用,则上面的行允许映射。

Credits @mudasobwa.

积分@mudasobwa。

PS. If your new and don't know where /etc/hosts is just type (in bash)

PS。如果你的新的并且不知道/ etc / hosts只是在哪里键入(在bash中)

sudo nano /etc/hosts

To open it and add the line.

打开它并添加该行。