Nodejs - Can't connect to Azure database with sequelize and pg - no pg_hba.conf entry for host

54 Views Asked by At

I have got an issue when trying to connect Azure postgres database from Nodejs app. Connecting by sequelize and pg lib give me same issue "no pg_hba.conf entry for host". After looking around for a while, I found solution for both libs from many sources. The root cause is that Azure db requires SSL connection by default, turning it off can resolve the issue but it's not secure. Adding ssl support from app is better choice.

With pg:

const { Client } = require('pg');
let client = new Client({
        connectionString: process.env.DB_URI + '/postgres',
        ssl: true,
    });

With sequelize, dialect option must be specified

export const sequelize = new Sequelize(process.env.DB_URI +  '/postgres', {
    dialect: 'postgres',
    dialectOptions: {
        ssl: {
            require: true
        }
    },
});
0

There are 0 best solutions below