Rails 4 - 如何在开发中使用 sqlite3,在生产中使用 Heroku 使用 PostgreSQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21122358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:16:13  来源:igfitidea点击:

Rails 4 - how to use sqlite3 in development and PostgreSQL in production w/Heroku

ruby-on-railspostgresqlherokusqliteruby-on-rails-4

提问by sixty4bit

I am trying to deploy to Heroku but can't because the default sqlite3 server is still in place.

我正在尝试部署到 Heroku,但无法部署,因为默认的 sqlite3 服务器仍然存在。

Detected sqlite3 gem which is not supported on Heroku. https://devcenter.heroku.com/articles/sqlite3

检测到 Heroku 不支持的 sqlite3 gem。 https://devcenter.heroku.com/articles/sqlite3

In another tutorial with Rails 3.2.13 I was able to use sqlite3 as the dev db and Postgres as the production db. The Gemfile looks different in Rails 4 but I have modified it to have this:

在 Rails 3.2.13 的另一个教程中,我能够使用 sqlite3 作为开发数据库和 Postgres 作为生产数据库。Gemfile 在 Rails 4 中看起来不同,但我已经修改了它:

group :development do
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

I then changed my database.yml file so that the production section looked like this:

然后我更改了我的 database.yml 文件,使生产部分看起来像这样:

production:
  adapter: postgresql
  database: my_production_database
  pool: 5
  timeout: 5000

I then ran bundle installand rake db:createand rake db:migratebut am still unable to push to Heroku. So I tried rake db:dropas well as rake db:createand rake db:migratebut am still getting the same error message.

然后我跑bundle installrake db:createrake db:migrate,但我仍然无法推到的Heroku。于是,我就rake db:droprake db:createrake db:migrate,但我仍然得到同样的错误消息。

Am I missing something? What else do I need to do to make sure I'm getting Postgres as my production database and am able to use Heroku?

我错过了什么吗?我还需要做什么来确保我将 Postgres 作为我的生产数据库并能够使用 Heroku?

回答by ChrisBarthol

Don't do it. You are just going to run into problems down the road. Use the same database in production and development. There are a lot of resources available in documenting the switch from a sqlite to postgres database.

不要这样做。你只会在路上遇到问题。在生产和开发中使用相同的数据库。有很多资源可用于记录从 sqlite 到 postgres 数据库的切换。

Take the time and switch.

抓紧时间换。

Have a look at this Rails Cast.

看看这个 Rails Cast。

http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast

http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast

回答by Justin

Try using this for your production DB

尝试将此用于您的生产数据库

production:
  adapter: postgresql
  host: localhost
  encoding: unicode
  database: my_production_database
  pool: 5
  username:
  password:

You can leave username and password blank

您可以将用户名和密码留空