从 Node.js 应用程序查询 Heroku 托管的 Postgres 数据库期间出现“自签名证书”错误

2024-01-13

我的 Node.js 应用程序能够通过以下方式使用本地 Postgres 数据库npm pg模块。 我可以通过命令行连接到 Heroku 托管的 Postgres 数据库(免费的爱好开发计划)heroku pg:psql命令也是如此。But当我的 Node.js 应用程序尝试查询 Heroku 托管的 Postgres 数据库时,我收到一个self signed certificate error.

这是输出self signed certificate error:

(node:2100) UnhandledPromiseRejectionWarning: Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1051:34)
    at TLSSocket.emit (events.js:189:13)
    at TLSSocket._finishInit (_tls_wrap.js:633:8)
(node:2100) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2100) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
D:\MY\DEV\PROJECTS\AdsSubscribeBot\test.js:57
  if (err) throw err;
           ^

Error: Connection terminated unexpectedly
    at Connection.con.once (D:\MY\DEV\PROJECTS\AdsSubscribeBot\node_modules\pg\lib\client.js:264:9)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (D:\MY\DEV\PROJECTS\AdsSubscribeBot\node_modules\pg\lib\connection.js:76:10)
    at Socket.emit (events.js:194:15)
    at TCP._handle.close (net.js:597:12)

重现此错误的最简单方法是尝试使用示例代码从 Heroku 开发中心连接 Node.js:https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js

这是导致的代码示例self signed certificate error:

const connectionString = 'postgres://USERNAME:PASSWORD@HOST:PORT/DB_NAME';

const { Client } = require('pg');

const client = new Client({
  connectionString: connectionString,
  ssl: true
});

client.connect();

client.query('SELECT * FROM users;', (err, res) => {
  if (err) throw err;
  for (let row of res.rows) {
    console.log(JSON.stringify(row));
  }
  client.end();
});

也许有人遇到过同样的问题并且知道如何解决它。

预先感谢您的任何帮助。


检查你的 pg 配置。听起来你正在使用 pg 8 ,它已弃用 隐式禁用证书验证(正如您在配置中将 ssl 设置为 true 但未提供 ssl 配置一样)。指定rejectUnauthorized: true需要有效的 CA 或rejectUnauthorized: false明确选择退出 MITM 保护。

您可以在设置 pg 配置的位置执行此操作,如下所示

const client = new Client({
  connectionString: connectionString,
  ssl: { rejectUnauthorized: false }
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Node.js 应用程序查询 Heroku 托管的 Postgres 数据库期间出现“自签名证书”错误 的相关文章

随机推荐