Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile

I'm new to Rails. This application works fine on my local machine, and deploys without any problem. But when I run heroku run rake db:migrate, I get this error:

Running `rake db:migrate` attached to terminal... up, run.1269
rake aborted!
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile.
like image 263
Sajad Rastegar Avatar asked Jul 15 '14 10:07

Sajad Rastegar


5 Answers

simply adding gem 'pg' to the gemfile didn't work for me.

This worked for me

gem 'pg', '~> 0.20'

Got this answer from

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

Thanks to Piers C

And yeah, gem 'rails_12factor' helps when it's time view Heroku's logs for error messages.

like image 120
user2576537 Avatar answered Nov 13 '22 21:11

user2576537


Add this line to your Gemfile inside the :production group (add one if you don't have it).

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

It's very clear from the error its self that gem pg needs to be added to your Gemfile. You might be using sqlite3 in your development but Heroku uses postgresql for their database.

Your Gemfile should look like this:

source 'https://rubygems.org'
ruby '2.0.0'


gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.0'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'

group :development, :test do
  gem 'sqlite3', '1.3.8'
  gem 'rspec-rails', '2.13.1'
  
end
group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'pg', '0.15.1'
  gem 'rails_12factor'
end
like image 20
Abhinay Avatar answered Nov 13 '22 23:11

Abhinay


I am using ruby 2.3.8. If you are unable to solve this issue, the problem can be with the version of pg you are trying to work with. Replacing gem 'pg' with gem 'pg', '~> 0.21' worked out well for me. Also, This is my database.yml file:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5
development:
  <<: *default
  database: <%= ENV['DB_NAME'] %>
  host: <%= ENV['DB_HOST'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  reconnect: true
test:
  <<: *default
  database: <%= ENV['POSTGRES_DB'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>
  username: <%= ENV['POSTGRES_USER'] %>
  host: <%= ENV['POSTGRES_HOST'] %>
  port: <%= ENV['POSTGRES_PORT'] %>
  min_messages: notice
production:
  <<: *default
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>

And this is my gemfile

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use postgresql as the database for Active Record
# Adding pg gem for postgres
gem 'pg', '~> 0.21'
# 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/rails/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:
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'

  # 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
like image 31
Parikshit Singh Avatar answered Nov 13 '22 22:11

Parikshit Singh


I got the error when upgrading gitlab. I executed the wrong command, saying sudo -u git -H bundle install --without postgres development test --deployment instead of sudo -u git -H bundle install --without mysql development test --deployment

Simply executing

sudo -u git -H bundle install --with postgres did it for me, in your case probably

bundle install --with postgres and afterwards the db migration, heroku run rake db:migrate

like image 39
matthaeus Avatar answered Nov 13 '22 22:11

matthaeus


You can also fix it by upgrading rails to a >= 5.1.5 version.

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

like image 34
andrewcockerham Avatar answered Nov 13 '22 23:11

andrewcockerham