You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js使用Sequelize和pg连接Azure PostgreSQL报错解决方案

解决Node.js连接Azure PostgreSQL时的“no pg_hba.conf entry for host”错误

在Node.js应用中使用pg或Sequelize连接Azure PostgreSQL时,若遇到“no pg_hba.conf entry for host”错误,根源在于Azure PostgreSQL默认强制要求SSL连接。虽然关闭SSL能临时解决问题,但会带来安全隐患,最优方案是在应用端配置SSL支持。

使用pg库的配置方式

直接添加ssl: true配置项即可:

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

使用Sequelize的配置方式

需要指定dialect为postgres,并在dialectOptions中开启SSL强制要求:

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

内容的提问来源于stack exchange,提问作者Cuong Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 02:42:07