Nodejs - 无法使用 Sequelize 和 pg 连接到 Azure 数据库 - 主机没有 pg_hba.conf 条目

问题描述 投票:0回答:1

我在尝试从 Nodejs 应用程序连接 Azure postgres 数据库时遇到问题。通过sequelize 和pg lib 连接给我同样的问题“主机没有pg_hba.conf 条目”。环顾了一段时间后,我从许多来源找到了这两个库的解决方案。根本原因是Azure db默认需要SSL连接,关闭它可以解决问题,但不安全。从应用程序添加 ssl 支持是更好的选择。

与PG:

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

使用sequelize时,必须指定方言选项

export const sequelize = new Sequelize(process.env.DB_URI +  '/postgres', {
    dialect: 'postgres',
    dialectOptions: {
        ssl: {
            require: true
        }
    },
});
node.js sequelize.js azure-database-postgresql
1个回答
0
投票

Nodejs - 无法使用 Sequelize 和 pg 连接到 Azure 数据库 - 主机没有 pg_hba.conf 条目。

问题的原因是 Azure 数据库 PostgreSQL 默认启用了 SSL。以及

databaseConnectionUri
方法在构造传递给 pg 模块的 URI 时不考虑 SSL 配置值。 pg 模块检查 URI 的查询字符串中是否有值为 true 或 1 的 SSL 密钥。

要解决这个问题,解决方案可以禁用

require_secure_transport
server 参数为 OFF .

enter image description here

如果不想禁用 SSL,您可以在连接字符串中添加 SSL 检查,如下所示:

ssl : true

另请参阅类似的SO 主题答案

© www.soinside.com 2019 - 2024. All rights reserved.