【问题标题】:Why can't I connect to the database?为什么我无法连接到数据库?
【发布时间】:2019-04-29 15:10:48
【问题描述】:

当我运行heroku run rails db:migrate 时,我得到一个错误:

PG::ConnectionBad: could not connect to server: No such file or directory
  Is the server running locally and accepting
  connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
/app/vendor/bundle/ruby/2.6.0/gems/pg-1.1.4/lib/pg.rb:56:in `initialize'
/app/vendor/bundle/ruby/2.6.0/gems/pg-1.1.4/lib/pg.rb:56:in `new'
/app/vendor/bundle/ruby/2.6.0/gems/pg-1.1.4/lib/pg.rb:56:in `connect'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:692:in `connect'

我已经安装了pg gem。这是我的配置:

# database.yml
production:
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  database: bigbig-fishfish_production
  username: bigbig-fishfish
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

我做的有什么明显错误的吗?为什么我会收到错误消息?

【问题讨论】:

    标签: heroku ruby-on-rails-5


    【解决方案1】:

    您没有提供host,因此 Rails 正在尝试连接到本地运行的 Postgres。您的数据库服务器不会在 Heroku 上的 localhost 上。此外,您的数据库名称和用户名看起来不正确。您不能自己选择这些值。

    假设您使用 Heroku's own Postgres service,您应该使用 DATABASE_URL environment variable 连接到 Postgres。由于您使用的是 Rails 5,您可能应该omit the database, username, and password values completely。确保你有一个DATABASE_URL(如果你使用 Heroku Postgres,你应该有一个开箱即用的)并且 Rails 应该自动使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2015-02-05
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多