-2

I have a ruby app running on heroku, I am trying to connect it to a Heroku postgres database. I have installed the add-on and bundled the 'pg' gem. I tried running

PG.connect(dbname:d6td9jdn7irk0u)

Only resulting in this:

PG::ConnectionBad: could not connect to server: No such file or directory. Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

My question is: how can I get the connection established correctly? should I supply the user/password to the connect() function?

Everything I can find when searching online is about people having a similar problem using ruby on rails

  • Possible duplicate of [PG::ConnectionBad - could not connect to server: Connection refused](https://stackoverflow.com/questions/19828385/pgconnectionbad-could-not-connect-to-server-connection-refused) – Jamie Counsell Jan 25 '18 at 22:39
  • Thank you for trying to help. This question is not a duplicate, I am not using ruby on rails or ActiveRecord. The problem is only occuring on heroku – Emil Bonne Kristiansen Jan 26 '18 at 00:36

2 Answers2

3

Heroku exposes their postgres connection string in DATABASE_URL environment variable. So you just PG.connect(ENV['DATABASE_URL']).

See https://devcenter.heroku.com/articles/connecting-to-heroku-postgres-databases-from-outside-of-heroku#credentials for details.

Stan Mazhara
  • 796
  • 4
  • 5
1

solved it, sort of. PG.connect() takes a hash which can include host/port/user/password information. All this information can be retrieved from the heroku cli using:

heroku pg:credentials:url database

unfortunately, the database credentials aren't permanent. So everytime I deploy I have to make sure the app was successfully deployed.