Heroku and Rails: Gem Load Error with Postgres, however it is Specified in GEMFILE
Try to write pg outside production, bundle, commit ur code with gemlock,gemfile and push code to heroku then try again
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile
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
postgresql gem is not loaded error deploying a Ruby on Rails application on Heroku
In your Gemfile
group :production do
gem 'pg'
end
Then run bundle install
and try to deploy on Heroku.
If you want to use PostgreSQL on all environments and not only in production
(recommended) add the gem outside the :production
group and remove other database adapters such as sqlite
.
As a side note, you may also want to add the rails_12factor
gem as suggested by Heroku.
Heroku with rails 5 error Gem::LoadError: Specified 'postgresql' for database adapter
If you have recently deployed your app to Heroku and in your attempt have been getting the "App crashed" error when you go to the Heroku app link, but everything else seems to be in order, it's most likely due to a new release of the 'pg' gem
. Postgres has released a new version of its gem which seems not fully compatible yet, so in your Gemfile under group production
change the line:
gem 'pg'
OR
gem 'pg', '~> 1.0.0'
to
gem 'pg', '~> 0.11'
OR
gem 'pg', '~> 0.20.0'
Note: the tilde sign before the >
, that's not a dash
Once you make this update in your group production of your Gemfile, ensure you run bundle install --without production
(to update Gemfile.lock file), do a git add/commit cycle, then re-deploy to Heroku.
For Good Practice
Use pg gem
one time without :group
because your database is same for both development
& production!
Rails 5.2.3 / Heroku - Why is it not possible to run postgres in production mode with squlite3 in development mode?
I fixed this by changing my config/database.yml to:
production:
adapter: postgresql
encoding: unicode
database: [my_db]
pool: 5
username: [myapp]
password: [password]
And running: heroku addons:create heroku-postgresql a- app_name
because PostgreSQL wasn't already provisioned for my app.
Heroku build error: Specified 'sqlite3' for database adapter, but the gem is not loaded
SQLite does not work with Heroku as its disk based and Heroku uses an ephemeral file system.
SQLite runs in memory, and backs up its data store in files on disk.
While this strategy works well for development, Heroku’s Cedar stack
has an ephemeral filesystem. You can write to it, and you can read
from it, but the contents will be cleared periodically. If you were to
use SQLite on Heroku, you would lose your entire database at least
once every 24 hours.Even if Heroku’s disks were persistent running SQLite would still not
be a good fit. Since SQLite does not run as a service, each dyno would
run a separate running copy. Each of these copies need their own disk
backed store. This would mean that each dyno powering your app would
have a different set of data since the disks are not synchronized.-Heroku Devcenter: SQLite on Heroku
Heroku provides Postgres as the free default database for rails which is as close to a recommendation as you can get.
If you are deploying to Postgres you should also be developing/testing on Postgres.
Differences between backing services mean that tiny incompatibilities
crop up, causing code that worked and passed tests in development or
staging to fail in production. These types of errors create friction
that disincentivizes continuous deployment. The cost of this friction
and the subsequent dampening of continuous deployment is extremely
high when considered in aggregate over the lifetime of an application.
- https://12factor.net/dev-prod-parity
If you really want to stick with SQLite you need to configure the adapters properly:
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
adapter: postgresql
# nothing else is needed
I do not recommend this.
Related Topics
How to Configure Capistrano to Use My Rvm Version of Ruby
"Whenever" Gem Running Cron Jobs on Heroku
Finding the Element of a Ruby Array with the Maximum Value for a Particular Attribute
Creating a Model That Has a Tree Structure
How to Alter the Timezone of a Datetime in Ruby
Differencebetween Link_To, Redirect_To, and Render
Ruby on Rails Rake Assets:Precompile Error
Problems Installing Ruby on Mountain Lion - Ruby 1.9.3 Wont' Compile
Changing Table Name at Query Run Time in a Rails Application
Unexpected Output in Ruby on Rails
Ubuntu Rails Install Fails on Zlib
Ruby on Rails Plural (Controller) and Singular (Model) Convention - Explanation
Checking If a Variable Is an Integer
How to Build a Rubygems Mirror Server
How to Calculate the Distance Between Two Gps Coordinates Without Using Google Maps API
Ruby Gemspec Dependency: Is Possible Have a Git Branch Dependency