rake aborted! Gem::LoadError: 为数据库适配器指定了'postgresql'。[英] rake aborted! Gem::LoadError: Specified 'postgresql' for database adapter

本文是小编为大家收集整理的关于rake aborted! Gem::LoadError: 为数据库适配器指定了'postgresql'。的处理方法,想解了rake aborted! Gem::LoadError: 为数据库适配器指定了'postgresql'。的问题怎么解决?rake aborted! Gem::LoadError: 为数据库适配器指定了'postgresql'。问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我是 Rails 新手.一切都很好,直到我更改了我的 Gemfile 以能够使用 heroku.我的 Gemfile 中有 pg,但在运行 bundle exec rake test

时仍然出现此错误

我也不能使用 rails 控制台.

    rake aborted!
Gem::LoadError: 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).
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:44:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/base.rb:316:in `<module:ActiveRecord>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/base.rb:26:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:411:in `maintain_test_schema!'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_help.rb:19:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/workspace/sample_app/test/test_helper.rb:3:in `<top (required)>'
/home/rihanna/workspace/sample_app/test/controllers/static_pages_controller_test.rb:1:in `require'
/home/rihanna/workspace/sample_app/test/controllers/static_pages_controller_test.rb:1:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Gem::LoadError: pg is not part of the bundle. Add it to Gemfile.
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/bundler-1.8.5/lib/bundler/rubygems_integration.rb:261:in `block in replace_gem'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:16:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:175:in `spec'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:44:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/base.rb:316:in `<module:ActiveRecord>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/base.rb:26:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:411:in `maintain_test_schema!'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_help.rb:19:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/workspace/sample_app/test/test_helper.rb:3:in `<top (required)>'
/home/rihanna/workspace/sample_app/test/controllers/static_pages_controller_test.rb:1:in `require'
/home/rihanna/workspace/sample_app/test/controllers/static_pages_controller_test.rb:1:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run

这是我的 Gemfile:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use sqlite3 as the database for Active Record

#gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem 'sqlite3'
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end


group :production do
  gem 'pg',             '0.18.1'
  gem 'rails_12factor', '0.0.2'

end

推荐答案

类似的问题,尽管 Gemfile 是正确的(生产中的 pg,dev 中的 sqlite,database.yml 有 postgresql).bundle 拒绝拾取 pg gem.

我正在编写 heroku 实例的创建脚本,所以所有这些都在 cli 上

我的解决方案

  • 显式添加 postgress 附加组件 (heroku addons:create heroku-postgresql:hobby-dev --app <myappname>).这似乎迫使它足够早地创建 DATABASE_URL (Created postgresql-xxxxxxx-9999 as DATABASE_URL).此时 bundle 似乎起作用了.

  • 我考虑/尝试并为每个环境创建保留的其他东西是确保您在流程中强制尽快设置变量heroku config:set --app <mayappname> RAILS_ENV=production

本文地址:https://www.itbaoku.cn/post/786673.html