When faced with this error when pushing a Rails 4 app to Heroku:
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
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?
$ heroku labs:enable user-env-compile
The gist is that the Heroku env variables are not set when your app begins to precompile the assets. Rails 4 is supposed to not load the full app during asset compilation, but sometimes it does anyway. Since those Heroku environment variables aren't set yet, like the location of the database or the Redis server, the app fails. That heroku labs command above makes the environment variables available early enough to prevent this specific brand of failure.
Here is an alternative solution.