0
votes

I am trying to connect to my PostgreSQL database running in Heroku which is setup using the postgres-addon. But when I do the application crashes (I have omitted my IP and the APPS URL):

$ heroku logs --tail
 ›   Warning: heroku update available from 7.42.0 to 7.42.2.
2020-07-29T08:53:16.308297+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/all" host=URL request_id=a80d2c63-af75-4ba4-9288-69148e2e5068 fwd="IP" dyno= connect= service= status=503 bytes= protocol=https

The way I connect to the database is

require 'pg'
class Database

  def initialize
    begin
      @conn = PG.connect env["DATABASE_URL"]
    rescue PG::Error => e
      puts e.message
    end
  end
...
end

As soon as PG.connect is called the app crashes and I have no idea why. From what I can tell I am using the right ENV variable.

Added info:

  1. I have realised if I use heroku pg:psql to connect to the database from CLI that works and I can make tables and insert entries.

  2. I tried to disable SSL mode that didn't work either

  3. I can connect from DBeaver

1
Are you able to successfully run CLI commands like heroku pg:info and heroku pg:psql? (The latter assumes you have the Postgresql tools installed locally.) - rmlockerd
Yes @rmlockerd I am able to do that - Lars Nielsen
Have you checked if the issue is caused by the sslmode parameter.? You could try setting the env variable PGSSLMODE=disable to see if this solve the issue and then find a way to fine-tune your sslmode in a more appropriate way. - Giuseppe Schembri
Will try this thanks @GiuseppeSchembri - Lars Nielsen

1 Answers

0
votes

I found a solution to the problem. If I add the authentication items (database name, port, passowrd, etc.) to the ENV variables and make a full configure:

@conn = PG.connect :dbname => USER, :user => USER, :password => PASSWORD, 
                   :host => HOST, :port => PORT

Then the connection works. However, the original PG.connect ENV['DATABASE_URL'] should work, but for some users.