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.


If it makes sense in your situation, you can choose against which environment assets:precompile should work, with the following command:

rake assets:precompile:all RAILS_ENV=development RAILS_GROUPS=assets

This make sense for my deployment because usually:

  1. I make rake to generates assets on my development machine (because the memory of my vps is somehow limited)
  2. I zip all the application with the assets generated in public/assets
  3. I transfer the zip to the vps ans unzip the package there

Hope it helps.