【问题标题】:rake aborted! Gem::LoadError: Specified 'postgresql' for database adapter on rails db:migrate耙中止! Gem::LoadError: 为 rails db:migrate 上的数据库适配器指定“postgresql”
【发布时间】:2017-10-18 22:04:27
【问题描述】:

宝石文件:

source 'https://rubygems.org'

gem 'rails',        '5.0.1'
gem 'bcrypt',       '3.1.11'
gem 'puma',         '3.4.0'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.0.0'
gem 'coffee-rails', '4.2.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.4.1'
gem 'bootstrap-sass', '3.3.6'
gem 'unicorn'

group :development, :test do
  gem 'sqlite3', '1.3.12'
  gem 'byebug',  '9.0.0', platform: :mri
end

group :development do
  gem 'web-console',           '3.1.1'
  gem 'listen',                '3.0.8'
  gem 'spring',                '1.7.2'
  gem 'spring-watcher-listen', '2.0.0'
end

group :test do
  gem 'rails-controller-testing', '0.1.1'
  gem 'minitest-reporters',       '1.1.9'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg', '0.18.4'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

数据库.yml:

default: &default
  adapter: sqlite3
  pool: 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

运行 rake db:migrate 时迁移失败。 尝试使用 RAILS_ENV=development 将环境变量设置为 development 仍然失败。我在生产中使用 postgres,因为 heroku 提供了 postgres。gem 文件和 database.yml 都给出了。

编辑: 我尝试将 repo 克隆到另一台机器上并尝试它工作正常。

【问题讨论】:

  • 谢谢大家,问题出在我的本地机器上,因为我最近安装了没有 brew 的 Postgres。一旦我卸载了 rhat 并重新启动,迁移就成功了。

标签: ruby-on-rails database-migration


【解决方案1】:

当你这样做时:

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

然后是这个:

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

那么,生产中的适配器是sqlite3。看看您是如何将其设置为默认值,然后又没有在生产环境中设置的?

还有,这一点:

production:
  ...
  database: db/production.sqlite3 << right here

似乎它会让你心痛。

您自然会意识到,不建议在开发中使用 sqlite,在生产中使用 postgresql。 Heroku 网站上这样说。因为,好吧,你将有你现在正在做的那种噩梦。 (Heroku 网站和 Innerwebs 周围有更多技术解释。阅读它们。这对灵魂有好处。)

除非你喜欢那种东西。

* 编辑 *

对不起,我走得太快了。运行rake db:migrate 时,您似乎在本地遇到问题?

无论如何,在开发和生产中使用不同的数据库仍然不是一个好主意。

【讨论】:

    【解决方案2】:

    尝试明确设置数据库配置以进行开发。看起来 database.yml 没有正确对齐。

    production:
      adapter: postgresql
      encoding: utf8
      database: your_db
      host: 127.0.0.1
      pool: 5
      username: your_db_username
      password: your_db_password
    

    【讨论】:

    • 我正在使用 sqlite 数据库进行开发
    • @Aditya 如果您对我的回答感到满意,请改正并投票
    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2014-08-11
    • 2015-01-13
    • 2014-01-01
    • 2015-12-04
    • 2014-09-05
    • 1970-01-01
    相关资源
    最近更新 更多