Rails rake assets:precompile for production

Kieran Klaassen picture Kieran Klaassen · Oct 16, 2011 · Viewed 49.9k times · Source

I'm trying to precompile the assets for my app to deploy to Heroku but have to following error.

When running:

RAILS_ENV=production bundle exec rake assets:precompile

Error:

/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted!
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.)

Because I use in development SQLite and in production Postgresql the following Gemfile

gem "rails", "~> 3.1.0"

group :production do
  gem 'pg'
end

group :development, :test do
  gem 'sqlite3'
end

gem 'sass-rails', "~> 3.1.0"

group :assets do
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
  gem 'compass', '~> 0.12.alpha.0'
  gem 'html5-boilerplate'
end

I tried a lot but can't get this working.

I don't know if this is important but my database.yml looks like:

production:
  adapter: postgresql
  host: localhost
  database: db
  encoding: unicode
  username: user
  password: ''

Answer

mraaroncruz picture mraaroncruz · Mar 22, 2013

Old question but the accepted answer doesn't really answer the question - and I just found this in a search so I guess it's relevant.

The reason for the error is that gem 'pg' is in the production gem group.
When you run rake assets:precompile the production environment is accessed. So it is trying to load the production environment but you don't have all of the dependencies installed.

Running RAILS_ENV=production bundle exec rails server would probably give you a similar error.

I can think of two different solutions

1) Look to see if you have a .bundle/config file in your app's root. If you do, check if it says WITHOUT :production or similar. Either remove that line or the whole .bundle directory and run bundle again.

2) in Gemfile

gem :development, :production do
  gem 'pg'
end

while removing the :production group
run bundle again

Sorry to bring up old stuff...