1

I'm trying to connect my Heroku App to an outside PostgreSQL. But I keep getting a connection refused error:

ActionView::Template::Error (could not connect to server: Connection refused
2018-08-30T12:44:46.868880+00:00 app[web.1]: Is the server running on       host "localhost" (127.0.0.1) and accepting
2018-08-30T12:44:46.868882+00:00 app[web.1]: TCP/IP connections on port 5432?

In my Heroku Admin I've set my DATABASE_URL to point to my PostgreSQL (No password)

enter image description here DATABASE_URL postgres://postgres:@localhost:5432/postgres

My database.yml looks like this

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

When I enter the command Heroku config I can definitely see my DATABASE_URL. What is the issue here?

SpongebobJunior
  • 601
  • 1
  • 9
  • 24
  • Why not use herokus Postgres dB? You can export your local the free hosted. – Timmy Von Heiss Aug 30 '18 at 12:59
  • We are experimenting the features of Heroku. The client might have their database separated – SpongebobJunior Aug 30 '18 at 13:02
  • By an an outside PostgreSQL you mean localhost? – muzychuk Aug 30 '18 at 13:08
  • yes. Outside of Heroku – SpongebobJunior Aug 30 '18 at 13:13
  • I think if you can change the heroku config `heroku config:set DATABASE_URL=postgres://user_name:password@host:port/database_name`. Make sure you're not behind firewall. – Kedarnag Mukanahallipatna Aug 30 '18 at 13:37
  • I already set it via the Heroku admin – SpongebobJunior Aug 30 '18 at 13:47
  • You can remove the line `url: <%= ENV['DATABASE_URL'] %>` from `database.yml`. Rails merges `ENV['DATABASE_URL']` with the values from `database.yml` anyways. https://guides.rubyonrails.org/configuring.html#configuring-a-database – max Sep 03 '18 at 05:33
  • Also have you tried connecting to your postgres server in any other way to eliminate possible issues such as an IP whitelist? You can also try detaching the postgres addon on heroku. https://stackoverflow.com/questions/35061914/how-to-change-database-url-for-a-heroku-application – max Sep 03 '18 at 05:36
  • I've already detached all Postgres addon on Heroku as well and still I'm getting a connection refused error. If I ran my Ruby and Rails on development environment and connect to Postgres it works fine. – SpongebobJunior Sep 03 '18 at 07:27

1 Answers1

3

DATABASE_URL postgres://postgres:@localhost:5432/postgres

That is setting the DATABASE_URL config var that is used in your app to use a database on localhost which will resolve to the machine the app is running on, hence the error.

What you probably want to do is change 'localhost' to be the IP address of where the Postgres instance is running along with credentials required to auth.

John Beynon
  • 37,398
  • 8
  • 88
  • 97
  • 1
    Already tried to replace localhost with my ip address and public address but still refuse to connect. Both heroku and postgres instance are in the same machine – SpongebobJunior Aug 30 '18 at 17:18
  • ah ok, so you're running postgres locally and trying to connect your Heroku app (running on Heroku - which runs on Amazon) to your local database? This points towards a networking config issue. – John Beynon Sep 04 '18 at 11:48