【问题标题】:Rails 5.2.3 / Heroku - Why is it not possible to run postgres in production mode with squlite3 in development mode?Rails 5.2.3 / Heroku - 为什么不能在生产模式下运行 postgres,而在开发模式下使用 squlite3?
【发布时间】:2019-05-31 17:26:18
【问题描述】:

我使用的是 Rails 5.2.3,当我尝试在 Heroku 管道上查看我的生产应用程序时遇到应用程序错误,并且在以下情况下出现以下错误:

  • 我运行heroku logs 我得到:heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/"

  • 我运行heroku run rails console 我得到:Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? sqlite3 is not part of the bundle. Add it to your Gemfile. (LoadError)

  • 我运行heroku run rails db:migrate 我得到:LoadError: Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? sqlite3 is not part of the bundle. Add it to your Gemfile.

我想使用 sqlite3 进行开发,使用 postgres 进行生产,那么为什么我需要让 sqlite3 gemfile 在生产模式下可用?

这是我的 gemfile:

group :development do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

这是我的 database.yml:

default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

【问题讨论】:

    标签: ruby-on-rails postgresql sqlite heroku


    【解决方案1】:

    你的问题是这部分:

    production:
      <<: *default
      database: db/production.sqlite3
    

    目前您的生产仍在尝试使用 sqlite,因为它扩展到

    production: 
      adapter: sqlite3
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      timeout: 5000
      database: db/production.sqlite3
    

    显然db/production.sqlite3 也建议使用sqlite 数据库

    将此部分改为

    production: 
      adapter: postgresql
      host: [your db host] 
      database: [your database] 
      username: [your username]
      password: [your password]
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      timeout: 5000
    

    PostgreSQL 所说的所有内容都是免费的,如果您要部署到 PostgreSQL,您真的不应该再次开发 sqlite。

    SQLite 非常适合概念验证,因为它可以快速扩展并且易于拆卸,没有真正的外部依赖,但是如果您想实际构建应用程序并将其部署在某个地方,您应该真正使用生产就绪数据库.

    Heroku DB config

    Heroku Getting started Rails 5

    【讨论】:

      【解决方案2】:

      database.yml 中的 production 设置继承了 development 设置 - 您想将其更改为

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

      url: &lt;%= ENV['DATABASE_URL'] %&gt; 适用于您使用 Heroku 的情况。如果不是可以考虑凭证规范格式here

      【讨论】:

        【解决方案3】:

        像这样试试你的 database.yml

        default: &default
          adapter: sqlite3
          pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
          timeout: 5000
        
        development:
          <<: *default
          database: db/development.sqlite3
        
        test:
          <<: *default
          database: db/test.sqlite3
        
        production:
          adapter: postgresql
          pool: 5
          timeout: 5000
          database: db/production.sqlite3
        

        【讨论】:

          【解决方案4】:

          我知道你写的

          我想使用 sqlite3 进行开发,使用 postgres 进行生产

          我能理解,因为我自己也去过那里;感觉 postgres 似乎是安装的一大步,“我只想要一些可以工作的小东西”。

          也许你的原因不同,但如果不是——安装 postgres 会为你省去很多麻烦。 Postgres 是免费的,默认安装设置可能对您很有效。

          除了为您省去适配器和 gem 不兼容的麻烦之外,您有时还会在可用的 SQL 命令(当您想做更高级的事情时)以及哪些东西具有高性能或不具有高性能方面存在差异。

          https://www.postgresql.org/download/

          当您拥有不同的开发和生产环境时 - 您将花费大量时间来修复不会增加您正在构建的任何东西的价值的东西。

          【讨论】:

            【解决方案5】:

            我通过将 config/database.yml 更改为:

            production:
              adapter: postgresql
              encoding: unicode
              database: [my_db]
              pool: 5
              username: [myapp]
              password: [password]
            

            并且正在运行:heroku addons:create heroku-postgresql a- app_name,因为尚未为我的应用配置 PostgreSQL。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-11-26
              • 2021-03-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多