使用sqlite3为Rails配置数据库,为postoku配置postgreSQL

时间:2017-09-04 00:34:45

标签: ruby-on-rails postgresql heroku sqlite

我在网上找了好几个小时,但我没有尝试调整我的database.yml。我刚开始使用Rails,练习是使用sqlite3进行开发,将postgreSQL用于生产。我正在使用rvm 2.4.1和rails 5.3.1。

我把它添加到我的gemfile:

group :production do
  gem 'pg'
end

这是我的database.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

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

# 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:
  <<: *default
  database: db/test.sqlite3

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

这样做不成功,当我尝试部署到heroku时出现以下错误:

Specified 'postgresql' for database adapter, but the gem is not loaded. Add 
`gem 'pg'` to your Gemfile (and ensure its version is at the minimum required 
by ActiveRecord).

我假设我需要以某种方式让生产部分导致postgreSQL,但heruko网站上的手册以及stackOverflow和其他地方的其他帖子都没有帮助我。我怎样才能让它发挥作用?我可以添加:默认为组,gem'pg'在我的Gemfile中,但是其他东西如scaffolding会在以后产生错误。我真的很感谢能够在这个问题上花费大约12个小时来获得具体的帮助。

1 个答案:

答案 0 :(得分:0)

首先,既然您使用postgresql通知了生产,那么您必须更改database.yml中的设置(参见生产部分)

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

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

# 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:
  <<: *default
  database: db/test.sqlite3

production:
  adapter: postgresql
  encoding: unicode
  database: project_name_production
  pool: 5
  host: localhost
  username: your_username
  password: your_database_password

然后您可以关注此rake command in heroku

关于你的宝石文件的第二个,如果问题仍然存在,那么你可以从生产环境中推出并推送到heroku,然后进行捆绑安装以安装适配器

的Gemfile

gem 'pg'

此处有助于您阅读以解决问题link information for heroku with postgresql