Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSL connection is required. Please specify SSL options and retry

I use azure postgres, so when I disable SSL it works fine but I want SSL connection enabled and when I enable from azure and then when I start my application ERROR: SSL connection is required. Please specify SSL options and retry This error occurs multiple time

where in docs of azure Azure Docs for SSL . here is my connection string

connection: {
        database: process.env.POSTGRES_DATABASE || 'postgres',
        port: process.env.POSTGRES_PORT || 5432,
        user: process.env.POSTGRES_USER || 'my@-username',
        host: process.env.POSTGRES_HOST || 'postgres.db.postgres.database.azure.com',
        password: process.env.POSTGRES_PASSWORD || 'postpass',
        sslmode: 'require',
        }

as per azure docs i specify that sslmode:require but its not working is anyone face the same error and got the solution then let me know, I stuck into this error from last 5 days.

like image 697
Kirtan Avatar asked Oct 19 '25 15:10

Kirtan


1 Answers

Regarding the issue, please refer to the following steps

  1. Download the SSL certificate. For more details, please refer to here

  2. Code

const pg = require("pg");
const fs = require("fs");
const config = {
  host: "<>.postgres.database.azure.com",
  user: "<>",
  password: "<>",
  database: "postgres",
  port: 5432,
  ssl: {
    cert: fs.readFileSync("<the cert path>"),
    rejectUnauthorized: true,
  },
};

const client = new pg.Client(config);

client.connect((err) => {
  if (err) throw err;
  else {
    console.log("success");
  }
});
const query = "SELECT * FROM inventory;";

client
  .query(query)
  .then((res) => {
    const rows = res.rows;
    console.log(`Read: ${JSON.stringify(rows)}`);
    process.exit();
  })
  .catch((err) => {
    console.log(err);
  });

enter image description here

like image 168
Jim Xu Avatar answered Oct 21 '25 06:10

Jim Xu