rake assets:precompile attempting to connect to database rake assets:precompile attempting to connect to database ruby ruby

rake assets:precompile attempting to connect to database


rake assets:precompile initializes your app by default, which includes connection to the database.

Inside config/application.rb you can add this, but see the link below for warnings about it:

config.assets.initialize_on_precompile = false

Rails Guide on Precompiling Assets


I had that same problem. After updating Sprockets to version 3, whenever I tried to precompile the assets locally (development), however using the settings of the production environment, I had this 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).

Because in my local (development) I use MySQL and in the server (production), I use Postgres.

The answer marked as resolved does not work for me, because config.assets.initialize_on_precompile is not available in Rails 4.2.1.

To solve, I followed 3 simple steps:

  1. In your Gemfile, add: gem "activerecord-nulldb-adapter"
  2. In database.yml, change the adapter as follow:

    production:  adapter: <%= ENV['DB_ADAPTER'] ||= 'postgresql' %>
  3. To compile your production assets locally. run in your terminal

    DB_ADAPTER=nulldb RAILS_ENV=production rake assets:precompile

This solutions solved to me and I sawyed here.


Newer versions of Rails do not load the database during asset precompile. However, initializers will often cause it to load. This can be frustrating to debug, because the error omits the stack trace.

./bin/rails assets:precompile(in ~/src/nautilus)rake aborted!ActiveRecord::NoDatabaseError: FATAL:  database "your_app_development" does not existbundler: failed to load command: rake (~/src/your_app/vendor/bundle/ruby/2.6.0/bin/rake)NoMethodError: undefined method `reject' for nil:NilClass

The less than helpful error.


So, how do we figure out where the problem is?

Move all the initializers out of config/initializers then run ./bin/rails assets:precompile.

If it works, great, you know the problem is in one of those files.

Move them back one by one till you find the offending file(s).


Now that the problem was found, how do we fix it?

Then when I find an initialilzer that has a problem I wrap it with the following check:

unless ARGV.include? "assets:precompile"  # ...end

I prefer this over the nulldb because it reduces the number of dependencies I have to maintain in the app.