Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Rake Asset Pipeline Failure

Error Syntax: Preparing app for Rails asset pipeline Running: rake assets:precompile Connecting to database specified by DATABASE_URL rake aborted! could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

Can anyone please tell me how to get through this error? Whenever I try to Deploy my RoR App on Heroku I get the same error everytime. Your Guidance is Required.

Regards

3 Answers

This is most likely a configuration issue (skip to the bottom to bypass the background information).

Heroku uses environment variables to configure how it connects to a database. What is usually configured in your config/database.yml file is overwritten by Heroku's slug compilation when you deploy. Its important to understand a that its the environment variable DATABASE_URL that contains all the information your Rails app will need to connect to a database once its running on Heroku. You can set and unset these environment variables using the Heroku toolkit’s config commands . The current values of your config variables are likely fine as is, but its their availability to the app while its precompiling your assets that’s important. I’ll get to more of that issue in a moment.

Heroku will precompile your assets (compiling sass and coffeescript and minifying js and css) during its slug compilation. This precompilation of assets creates static js and css files and frees up the web server from doing this when a page is later requested from the app. The precompilation can be done locally as well using the rake assets:precompile task, though running this task locally you wouldn't encounter an error because the database configuration is still being found in config/database.yml.

The gotcha with Heroku’s slug compilation running the assets:precompile task is that the environment variables you set aren’t made available to the app at that moment. So even if you have a correct DATABASE_URL set, your app doesn’t have access to it at that moment. The indicator of this problem is that its trying to connect to a database on 127.0.0.1, which is localhost, which is the default if no database host is declared. Without the environment variables, your app thinks you haven’t told it a non-default database host to connect to.

Luckily there is a workaround for this. Asset precompilation doesn’t necessarily require the rest of the app to have started up in order to do its work. Though it does allow the app to start up by default, you can turn this off using a configuration in your config/application.rb file. Add this to that file in your app:

config.assets.initialize_on_precompile = false

This will keep the app from starting up and connecting to a database it can’t find when running assets:precompile. There’s more information on this from Heroku here. Every app you deploy to Heroku will need to have this configuration set.

If this doesn’t solve the problem we’ll then look into some other possibilities, but this is most often the cause with this type of error.

Chris Gray
Chris Gray
10,305 Points

Have you managed to deploy this before now or is this the first time running it?

Regards

Dear Sir I have manged to deploy some of my apps before but with most of my apps I get the same error. Please guide me how to get through it. Regards

Chris Gray
Chris Gray
10,305 Points

Ok, can you either send me the link to the github repo or paste in your gemfile, this problem usually comes from there