How to solve SSL off on Heroku Postgres

When you are working on a Node/Express application that connects to a Postgres database and deploys it to Heroku, you may face this error when trying to interact with the production database.

error: no pg_hba.conf entry for host, SSL off

What does this error mean?

  • This could happen if you are trying to connect to a database with the wrong credentials.
  • Heroku Postgres requires an SSL connection to ensure that communication between applications and databases remains secure. If you have not enabled an SSL connection to your application you may face this error even if you are using the right credentials.

How to enable an SSL connection?

It is simple with only a few lines of code you can enable an SSL connection to your Heroku Postgres.

In your code where you have configured a connection to a Postgres database add another parameter with these codes next to your connection

dialectOptions: {
    ssl: {
      require: true,
      rejectUnauthorized: false,
    },
  }

This will solve the issue, but sometimes you might still face the issue even if you have added those codes, by adding this parameter ssl: true, it will solve completely the error. For example:

  ssl: true,
  dialectOptions: {
    ssl: {
      require: true,
      rejectUnauthorized: false,
    },
  }

Hope this will help someone who is facing this error.