Sumit Bisht
Sumit Bisht

Reputation: 1517

How to use postgres with rails on heroku

I am having the following contents in a database.yml

production:
  adapter: postgresql
  database: dbname
  host: host.compute-1.amazonaws.com
  username: user
  password: pass_word
  DATABASE_URL: postgres://user:[email protected]:5432/dbname

However on a git push heroku master, I am getting the following error.

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?

Do I need changes for other environments as well?

Upvotes: 2

Views: 406

Answers (3)

Neil Middleton
Neil Middleton

Reputation: 22238

This error

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?

is emitted during push, because be default Rails will spin up to precompile assets, and part of that is Rails trying to connect to the database. During build time, your application configuration is not present, hence why Rails tries to fall back to the default of using localhost (which doesn't exist).

There's a couple of ways to stop this happening. One (the recommended way) is to set

config.assets.initialize_on_precompile = false

in your production.rb.

A second way, is to enable the user-env-compile labs feature that will allow your PG config to exist at build time.

$ heroku labs:enable user-env-compile -a <YOUR_APP_NAME>

Upvotes: 0

Debadatt
Debadatt

Reputation: 6015

heroku autogenerates database.yml on the deploy.

You need to .git ignore database.yml

You may choose another database by setting the ENV['DATABASE_URL'] (use heroku config:add DATABASE_URL=....)

For more information please check the heroku documentation

Upvotes: 3

Amrit Dhungana
Amrit Dhungana

Reputation: 4485

DATABASE_URL is not required. Or you can check this one http://railsapps.github.io/rails-heroku-tutorial.html

Upvotes: 0

Related Questions