【问题标题】:Switched my Rails database to PostgreSQL for Heroku, and now the app can't access the DB at all将我的 Rails 数据库切换到 Heroku 的 PostgreSQL,现在应用程序根本无法访问数据库
【发布时间】:2020-01-06 23:41:18
【问题描述】:

我真的被这个难住了。我已经尝试了很多东西。

我不得不从 SQLite3 切换到 PostgreSQL,这样我才能在 Heroku 上进行部署。当我在本地运行它时一切正常。在我 git push heroku master 之后发生了一个错误。它开始部署,我得到了这个:

Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the tasks for the non-primary databases. If you'd like to use this feature, please simplify your ERB.

当我运行 heroku rake 时也会发生这种情况,只是后面跟着这个:

rake aborted! Psych::BadAlias: Cannot load database configuration: Unknown alias: default

随后是堆栈跟踪。

我想我一路上搞砸了。我只是不知道它在期待什么,或者我应该在哪里寻找。我的 database.yml 看起来像这样:

production: adapter: postgresql encoding: utf8 pool: 15 <<: *default url: *db url from heroku* timeout: 5000

非常感谢任何帮助。我对这一切都很陌生。我试过删除和重置数据库。我能提供的最后一点证据是 Heroku 的日志:

heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=ohmydog.herokuapp.com request_id=6217a180-f9ef-43da-8bb0-fc5d064e11fb fwd="185.232.22.206" dyno= connect= service= status=503 bytes= protocol=https

【问题讨论】:

  • 嗨,Brian,您能发布完整的 database.yml 文件吗?这个想法是查看“默认”条目。
  • 谢谢,下面的评论有助于消除该错误,但我看到另一个错误,所以我会在那里发布

标签: ruby-on-rails postgresql heroku heroku-postgres


【解决方案1】:

实际上,您几乎不需要进行任何配置才能让 Postgres 在 Heroku 上运行,因为它通过 ENV["DATABASE_URL"] 提供了几乎整个配置,它会自动与 database.yml 中的设置合并(并优先于)。

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 15 } %>

development:
  <<: *default
  database: my_app_development

test:
  <<: *default
  database: my_app_test

production:
  <<: *default

这实际上是 Postgres.app 本地和 Heroku 的功能齐全的配置。我真的会把几乎所有东西都保留为默认值,如果需要,只保留tune the DB。更喜欢在本地使用 ENV["DATABASE_URL"] 以及设置密码和用户名等内容,因为它可以避免开发人员大战。

【讨论】:

  • 谢谢!我之前实际上能够做到这一点,但我收到以下错误:ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist 2020-01-07T00:03:44.787597+00:00 app[web.1]: LINE 8: WHERE a.attrelid = '"users"'::regclass 2020-01-07T00:03:44.787600+00:00 app[web.1]: ^ 这也来自我的 Heroku 日志
  • PG::UndefinedTable 错误通常告诉您忘记运行创建表的迁移。
  • 你必须使用heroku run rails db:migrate在本地和heroku上运行迁移。
  • 我知道我错过了一些东西。太感谢了!我认为这是更大的事情,哈哈。
猜你喜欢
  • 1970-01-01
  • 2013-04-05
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多