Raaz
Raaz

Reputation: 1751

How do i connect my postgres db in heroku?

I am learning rack and I built a test todo app. My test app uses postgres db. I am trying to deploy my app to heroku free account.

Currenlty I am trying to connect to postgres using this

DB = PG.connect :hostaddr => "localhost", :port => 5432, :dbname => 'testdb', :user => "postgres", :password => "postgres"

I checked my logs because my app was not workign when I visited the url and found out

2017-06-01T13:22:35.540907+00:00 app[web.1]: Puma starting in single mode...
2017-06-01T13:22:35.540929+00:00 app[web.1]: * Version 3.8.2 (ruby 2.3.4-p301), codename: Sassy Salamander
2017-06-01T13:22:35.540930+00:00 app[web.1]: * Min threads: 5, max threads: 5
2017-06-01T13:22:35.540975+00:00 app[web.1]: * Environment: production
2017-06-01T13:22:35.583653+00:00 app[web.1]: ! Unable to load application: PG::ConnectionBad: could not connect to server: Connection refused
2017-06-01T13:22:35.583655+00:00 app[web.1]:    Is the server running on host "127.0.0.1" and accepting
2017-06-01T13:22:35.583656+00:00 app[web.1]:    TCP/IP connections on port 5432?
2017-06-01T13:22:35.583775+00:00 app[web.1]: bundler: failed to load command: puma (/app/vendor/bundle/ruby/2.3.0/bin/puma)
2017-06-01T13:22:35.583838+00:00 app[web.1]: PG::ConnectionBad: could not connect to server: Connection refused
2017-06-01T13:22:35.583839+00:00 app[web.1]:    Is the server running on host "127.0.0.1" and accepting
2017-06-01T13:22:35.583840+00:00 app[web.1]:    TCP/IP connections on port 5432?

Edit 1

I found the postgres db creds in the heroku and used that and got this err

2017-06-01T14:27:11.889525+00:00 app[web.1]: Puma starting in single mode...
2017-06-01T14:27:11.889599+00:00 app[web.1]: * Version 3.8.2 (ruby 2.3.4-p301), codename: Sassy Salamander
2017-06-01T14:27:11.889633+00:00 app[web.1]: * Min threads: 5, max threads: 5
2017-06-01T14:27:11.889671+00:00 app[web.1]: * Environment: production
2017-06-01T14:27:11.934347+00:00 app[web.1]: ! Unable to load application: PG::ConnectionBad: could not translate host name "ec2-23-23-234-118.compute-1.amazonaws.com" to address: Name or service not known
2017-06-01T14:27:11.934473+00:00 app[web.1]: bundler: failed to load command: puma (/app/vendor/bundle/ruby/2.3.0/bin/puma)
2017-06-01T14:27:11.934522+00:00 app[web.1]: PG::ConnectionBad: could not translate host name "ec2-23-23-234-118.compute-1.amazonaws.com" to address: Name or service not known

Upvotes: 1

Views: 309

Answers (2)

Raaz
Raaz

Reputation: 1751

Using this solved it DB = PG.connect ENV["HEROKU_POSTGRESQL_SILVER_URL"]

Upvotes: 0

DiegoSalazar
DiegoSalazar

Reputation: 13521

You can get your database config on Heroku from the DATABASE_URL environment variable:

ENV['DATABASE_URL']

That is a connection URL which looks like this:

postgres://$user:$pass@$db_host.com:5432/$db_name

You can pass that string directly to your PG client initializer:

DB = PG.connect ENV['DATABASE_URL']

DB is now your connected db client.

Upvotes: 1

Related Questions