我的Node.js应用程序可以通过以下方式使用本地Postgres数据库
模块。我可以通过命令行连接到Heroku托管Postgres数据库(免费的Hobby Dev计划)
命令。
但是
当我的Node.js应用程序尝试查询Heroku托管Postgres数据库时,我收到一个
错误。
下面是输出
错误:
(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. (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 devcenter连接到Node.js:
https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js
以下是导致以下问题的代码示例
错误:
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();
});
也许有人曾经面对过同样的问题,并且知道如何解决它。
提前感谢您的帮助。
发布于 2020-04-10 00:28:01
检查您的pg配置。听起来您使用的是PG8,它不建议隐式禁用证书验证(就像您在配置中所做的那样,其中ssl设置为true,但没有提供ssl配置)。指定
要要求有效的CA或
明确选择退出MITM保护。
您可以在设置pg配置的位置执行此操作,如下所示
const client = new Client({
connectionString: connectionString,
ssl: { rejectUnauthorized: false }
})
发布于 2021-02-28 20:20:43
以下是使用Knex.js的公认答案的变体。在Heroku上测试过。
const parse = require('pg-connection-string').parse;
const pgconfig = parse('your-pg-connection-string');
pgconfig.ssl = { rejectUnauthorized: false };
const knex = Knex({
client: 'pg',
connection: pgconfig,
});
https://stackoverflow.com/questions/61097695
复制相似问题