【问题标题】:Connecting to azure flexible postgres server via node pg通过节点 pg 连接到 azure 灵活的 postgres 服务器
【发布时间】:2021-09-07 18:40:08
【问题描述】:

我在 Azure 上使用免费订阅,并成功创建了一个 Ubuntu 服务器和一个灵活的 Postgres 数据库。

直到最近,我还是直接从我的 Windows 10 桌面访问数据库。现在我想通过 Ubuntu 服务器路由所有访问。

为此,我在我的 Windows 10 机器上安装了 Open SSH Client 和 Open SSH Server,并使用 ssh -L 12345:[DB IP]:5432 my_user@[Ubuntu IP] 完成了必要的本地端口转发

连接有效,我在我的桌面上使用 pgcli 确认了它pgcli -h 127.0.0.1 -p 12345 -u my_user -d my_db

但是当我尝试通过 node-pg 连接时,我收到以下错误

UnhandledPromiseRejectionWarning: error: no pg_hba.conf entry for host "[Ubuntu IP]", user "my_user", database "my_db", SSL off

我已经使用 [Ubuntu IP] 在 Azure 中添加了防火墙规则,但错误仍然存​​在。更让我烦恼的是,在数据库的 Azure 门户中,我启用了“允许从 Azure 中的任何 Azure 服务公开访问此服务器”,因此此连接甚至不需要额外的防火墙。

上周,我一直坚持这一点,现在终于建立了连接,但我的代码无法访问。相当令人沮丧。对于如何解决此问题的任何指示,我都会很高兴。

编辑#1:

我无法发布 pg_hba.conf 文件。因为 Postgres DB 是由 Azure 管理的,所以我无法访问 pg_hba,这使得情况更加难以理解。

我用于测试连接的 node.js 代码:

const pg = require("pg");
const passwd = "...";

const client = new pg.Client({
    user: 'admin',
    host: '127.0.0.1',
    database: 'test',
    password: passwd,
    port: 12345
});
client.connect()

client.on('uncaughtException', function (err) {
    console.error(err.stack);
});

const query = "SELECT * FROM test";
try {client.query(query, (err,res) => {
    if (err) {
        console.error(err);
    }
    console.log(res);
})}
catch (e) {
    console.error(e)
}

【问题讨论】:

  • 天蓝色服务器pg_hba坚持使用ssl,而node坚持使用ssl。一个人必须向另一个人屈服。您既没有显示客户端的节点连接代码,也没有显示服务器的 pg_hba,但看起来其中一个需要更改。
  • 谢谢 jjanes,这极大地帮助了我理解正在发生的事情。我将编辑有关 pg_hba 和我的节点代码的问题,然后发布我找到的解决方案。

标签: node.js postgresql azure azure-sql-database portforwarding


【解决方案1】:

@jjanes 的评论帮助我理解了这个问题,谢谢。

这个编辑后的 ​​pg.Client 配置解决了我的问题:

const client = new pg.Client({
    user: 'admin',
    host: '127.0.0.1',
    database: 'test',
    password: passwd,
    port: 12345,
    ssl: {rejectUnauthorized: false}
});

我在这里找到了这个特定的 SSL 选项 https://node-postgres.com/features/ssl

【讨论】:

    猜你喜欢
    • 2021-11-25
    • 2014-03-11
    • 2019-12-17
    • 1970-01-01
    • 2013-11-15
    • 2018-08-14
    • 1970-01-01
    • 2018-07-20
    • 2022-01-25
    相关资源
    最近更新 更多