'Strapi v4 PostgreSQL connection from local project to azure db

Trying to connect to an Azure PostgreSQL server from my local Strapi project (eventually deployed in a docker container).

I have the connection configured according to the strapi docs, including using the cert downloaded from the azure portal:

https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/required/databases.html#configuration-structure

my database.js file:

// PostgreSQL
////////////////////////////////////////

const fs = require("@strapi/strapi/lib/services/fs");

const parse = require("pg-connection-string").parse;
const db = parse("azure-conn-string");

module.exports = ({ env }) => ({
  connection: {
    client: "postgres",
    connection: {
      host: db.host,
      port: db.port,
      database: db.database,
      user: db.user,
      password: db.password,
      ssl: {
        ca: fs.readFileSync(`${__dirname}/db.crt.pem`).toString(),
      },
    },
  },
});

when starting the server i get this in the terminal:

error: no pg_hba.conf entry for host "ip-address", user "username", database "db_name", SSL off
    at Parser.parseErrorMessage (/Users/x/x/dockertest/node_modules/pg-protocol/dist/parser.js:287:98)
    at Parser.handlePacket (/Users/x/x/dockertest/node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (/Users/x/x/dockertest/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (/Users/x/x/dockertest/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (events.js:400:28)
    at Socket.emit (domain.js:475:12)
    at addChunk (internal/streams/readable.js:293:12)
    at readableAddChunk (internal/streams/readable.js:267:9)
    at Socket.Readable.push (internal/streams/readable.js:206:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)

It seems like its trying to connect with ssl off, I'm not sure why since it should be configured to be enabled. If I remove the ssl rule from the azure database, the connection will go through so it seems like the problem is something with the ssl config.

Can anyone help?



Solution 1:[1]

   module.exports = ({ env }) => ({
  defaultConnection: "default",
  connection: {
    client: "postgres",
    connection: {
      host: env("DATABASE_HOST", "localhost"),
      port: env.int("DATABASE_PORT", 5432),
      database: env("DATABASE_NAME", "postgres"),
      user: env("DATABASE_USER", "postgres"),
      password: env("DATABASE_PASSWORD", "0000"),
      schema: env("DATABASE_SCHEMA", "public"),
    },
  }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1