Unable to connect staging env - [DatabaseConnectError: Unable to connect to the given uri] / [Connection error: no pg_hba.conf entry for host]

For people interested in such a topic, when you face that kind of error with a remote database, it is often an issue regarding connecting to your database using ssl. Especially if you are sure that the user you attempt to connect with exists and has sufficient rights to connect.

In that regard, playing around with the sslMode option and the ssl one is the way to go. Most of the time, your database want to enforce ssl connection so using the following in your datasource creation code will do the trick:

ssl: true,
sslMode: "required" // or "verify"

That way, ssl connection with the database will be enforced.

The complete list of available configuration for the ssl connection is available here.

In the hope that it might help someone.

Steve.