【问题标题】:Problems with rails serverrails服务器的问题
【发布时间】:2014-03-08 13:17:37
【问题描述】:

我刚刚开始使用 ruby​​ on rails 和 heroku。我正在关注 herokus 网站上的入门指南,但遇到了问题。我已经登录到heroku,创建了一个应用程序rails new myapp --database=postgresql,然后在app/views/welcome/index.html.erb 中创建了一个控制器rails generate controller welcome index,我刚刚创建了一个标题,说你好世界。当我启动本地服务器并转到它时,我得到could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? 并在终端中列出了下面列出的输出像在 heroku 教程中一样创建了一个控制器,一切正常,所以我认为问题出在rails new myapp --database=postgresql 我该如何解决这个问题?

PG::ConnectionBad (could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
):
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:831:in `initialize'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:831:in `new'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:831:in `connect'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:548:in `initialize'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
  /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
  /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
  activerecord (4.0.2) lib/active_record/connection_handling.rb:79:in `retrieve_connection'
  activerecord (4.0.2) lib/active_record/connection_handling.rb:53:in `connection'
  activerecord (4.0.2) lib/active_record/query_cache.rb:51:in `restore_query_cache_settings'
  activerecord (4.0.2) lib/active_record/query_cache.rb:43:in `rescue in call'
  activerecord (4.0.2) lib/active_record/query_cache.rb:32:in `call'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.2) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.2) lib/active_support/callbacks.rb:373:in `_run__4242016335734095215__call__callbacks'
  activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.2) lib/rails/engine.rb:511:in `call'
  railties (4.0.2) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'


  Rendered /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)
  Rendered /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
  Rendered /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /Users/mattmoss/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (18.2ms)

这是我的 database.yml

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
development:
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: postgresql
  encoding: unicode
  database: myapp_test
  pool: 5
  password:

production:
  adapter: postgresql
  encoding: unicode
  database: myapp_production
  pool: 5
  password:

development:
  host: localhost

test:
  host: localhost

【问题讨论】:

  • 从这里开始......并确保您在本地运行 Postgres,stackoverflow.com/questions/7975556/…
  • “没有这样的文件或目录”通常表示套接字问题。
  • 我该如何解决这个问题(我昨天开始学习)@PeterWooster
  • 查看我的回答,但我不是 Postgres 专家,请查找有关无法连接到 localhost 的问题。

标签: ruby-on-rails ruby postgresql heroku


【解决方案1】:

在大多数 *nix 服务器上,“localhost”是通过 Unix 风格的套接字处理的。如果您改用 127.0.0.1,它将使用 TCP/IP。尝试一下,或者确保您的客户端和服务器使用相同的套接字地址,并且它实际上是一个可用的 *nix 套接字。

database.yml 中有一个配置参数可让您设置套接字地址,并确保您也可以访问该目录。

【讨论】:

    【解决方案2】:

    在你的config/database.yml,尝试添加

    host: localhost
    

    在您的每个开发和测试环境中或更具体地查看您现有的config/database.yml,如上所示:

    development:
      adapter: postgresql
      encoding: unicode
      database: myapp_development
      pool: 5
      password:
      host: localhost
    
    test:
      adapter: postgresql
      encoding: unicode
      database: myapp_test
      pool: 5
      password:
      host: localhost
    

    希望对你有帮助

    【讨论】:

    • 添加后,当我转到localhost:3000时,它会显示database configuration does not specify adapter
    • 您可以将原始config/database.yml 文件添加到您的问题中吗?
    • 您不需要添加单独的development:,只需在您已有的development: 下方添加host: localhost 在您已经拥有的config/database.yml 文件的开头,我已经编辑了我的答案显示这个
    • 好的,现在我收到could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (fe80::1) and accepting TCP/IP connections on port 5432?
    • 对不起,我不确定你的问题是什么,我会仔细检查你的 mac 上安装 postgres 的方式,在你的 mac 上安装 postgres 的一种方法是使用postgresapp.com,抱歉我无法提供更多帮助
    【解决方案3】:

    我听说 Heroku 对数据库连接行为进行了一些更改。对于 Rails 版本 >= 4.1,它不会覆盖您的 config/database.yml - 它会与它合并。首先您需要访问https://postgres.heroku.com/databases 并单击您的数据库。您应该在下图中查看该参数

    像这样使用该变量作为您的生产参数

    production:
      adapter: postgresql
      url: <%= ENV['HEROKU_POSTGRESQL_BLACK_URL'] %>
    

    在尝试不同的事情后,这对我有用。我希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-03-31
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多