Rails:将我的开发数据库从sqlite更改为postgresql

时间:2016-05-27 16:28:57

标签: ruby-on-rails postgresql sqlite

我有一个Rails应用程序,目前在开发中使用sqlite,在Heroku上使用postgresql。但是,我试图改变它,以便我在开发中使用postgresql。

My Gemfile目前看起来像这样:

source 'https://rubygems.org'

gem 'rails',                '4.2.2'
gem 'bcrypt',               '3.1.7'
gem 'bootstrap-sass',       '3.2.0.0'
gem 'sass-rails',           '5.0.2'
gem 'uglifier',             '2.5.3'
gem 'coffee-rails',         '4.1.0'
gem 'jquery-rails',         '4.0.3'
gem 'jquery-ui-rails', '~> 4.2.1'
gem 'turbolinks',           '2.3.0'
gem 'jquery-turbolinks'
gem 'jbuilder',             '2.2.3'
gem 'sdoc',                 '0.4.0', group: :doc
gem 'chart-js-rails'
gem 'gon'
gem 'lodash-rails'

group :development, :test do
  #gem 'sqlite3',     '1.3.9'
  gem 'pg',             '0.17.1'
  gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
  gem 'puma',           '3.1.0'
end

因此,在开发小组中,我已将'sqlite3' gem替换为'pg' gem

我已经修改了database.yml,看起来像这样:

default: &default
  adapter: postgresql
  host: localhost
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: abc_development
  username: abc
  password: password1

test:
  <<: *default
  database: abc_test
  username: abc
  password: password1

production:
  <<: *default
  database: abc_production
  username: abc
  password: password1

我已经执行了bundle install --without production但是,当我尝试bundle exec rake db:create db:migrate时,我收到以下错误:

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).

1 个答案:

答案 0 :(得分:2)

到目前为止,我看到Rails是对的:你只在组pg中指定了production gem,但是你说你也想在开发中使用它。

因此,只需将其从production组移至Gemfile

的开头即可