Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku and Rails: Gem Load Error with Postgres, however it is Specified in GEMFILE

So I am trying to deploy a small project to Heroku, however when I deployed it and tried to run heroku run rake db:migrate it returned me the following error:

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).
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:50:in `establish_connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:27:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
/app/config/environment.rb:5:in `<top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/application.rb:328:in `require_environment!'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/application.rb:457:in `block in run_tasks_blocks'
Gem::LoadError: pg is not part of the bundle. Add it to Gemfile.
/app/vendor/bundle/ruby/2.2.0/gems/bundler-1.11.2/lib/bundler/rubygems_integration.rb:304:in `block in replace_gem'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:16:in `<top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:175:in `spec'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:50:in `establish_connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:27:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
/app/config/environment.rb:5:in `<top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/application.rb:328:in `require_environment!'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/application.rb:457:in `block in run_tasks_blocks'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

I can see from the first line that I need to load the pg gem in my gem file, however I already have it loaded in the production group and installed through bundle. Here is my GEMFILE:

source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'devise', '~> 3.5', '>= 3.5.6'
gem 'paperclip', '~> 4.3', '>= 4.3.6'
gem 'materialize-sass'
gem "font-awesome-rails"
gem 'bootstrap-sass', '~> 3.2.0'
gem 'autoprefixer-rails'
gem 'ratyrate'

group :development, :test do
    gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

group :development do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

Before posting this question I have read the answers to the same question (rake about database adapter inconsistent with database.yml, rake aborted! Gem::LoadError: Specified 'postgresql' for database adapter, Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded) using the answers there, the error still occurred.

Thanks for helping :)

like image 746
Mattias Zurkovic Avatar asked Sep 01 '16 02:09

Mattias Zurkovic


2 Answers

I had this exact issue and resolved it by changing to:

'pg', '~> 0.20'
like image 147
Piers C Avatar answered Oct 12 '22 15:10

Piers C


Should also be fixed by upgrading rails >= 5.1.5

https://github.com/rails/rails/issues/31673

like image 41
andrewcockerham Avatar answered Oct 12 '22 13:10

andrewcockerham