Error Loading the 'Sqlite3' Active Record Adapter. When I Deploy in Heroku

Error loading the 'sqlite3' Active Record adapter. when I deploy in Heroku

You should remove SQLite from your application and use Postgres in development, testing and production. There are many small incompatibilities between RDBMS's and you don't want to discover them when you push your app to production.

1. Install Postgres on the local system.

How to do this depends on your system. OS-X and Windows have simple installers. On Linux you would install it through your package manager.

2. Remove the sqlite gem.

# remove
gem 'sqlite3', '~> 1.3.6'

# add
gem 'pg', '~> 0.18.4' # check rubygems.org for the latest version

Run bundle update to regenerate the Bundle.lock.

3. Configure database.yml

default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
<<: *default
database: 'my_app_development'

# 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: 'my_app_test'

# On Heroku you don't need the production section as it provides everything through
# ENV["DATABASE_URL"]
# Heroku sets pretty good defaults as well so YAGNI.

4. Commit and push

Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? sqlite3 is not part of the bundle. Add it to your Gemfile for heroku

@petertran98 SQLite is a bad fit for running on Heroku. See the official link by heroku SQLite on Heroku and they recommend using Postgres.

To replace SQLite with Postgres, you need to do the folowwing.

Step - 1: Replace gem 'sqlite3' with gem 'pg' in your Gemfile.

Step - 2: Replace adapter: sqlite3 with adapter: postgresql in your database.yml.

Now try running the deployment. It should do the trick. If you still face some issue then you might need to do 1 of following things on the basis of what error you see.

Either you need to manually add Postgres if that does no gets created automatically with deployment script.

heroku addons:create heroku-postgresql

or if you still see the SQLite error then you might need to delete Gemfile.lock file and run bundle install command again.

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.

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.



Related Topics



Leave a reply



Submit