首页 > 解决方案 > (节点:5321)UnhandledPromiseRejectionWarning:错误:用户“postgres”的密码验证失败

问题描述

我正在参加网络开发课程,目前正在与此视频一起编码:https ://www.youtube.com/watch?v=RhJwCD0tT5Y

我已经为应用程序创建了一个 github 存储库,可以在这里找到:https ://github.com/JohnTarvis/node-pg-demo

无论我是以普通用户名、postgres 还是 root 登录,都会出现同样的错误。

这是完整的错误:

(node:6267) UnhandledPromiseRejectionWarning: error: password authentication failed for user "postgres"
    at Parser.parseErrorMessage (/home/tarvis/Documents/sb-exercises/codealong/express-pg-intro-demo/VideoCode/pg-intro/node_modules/pg-protocol/dist/parser.js:287:98)
    at Parser.handlePacket (/home/tarvis/Documents/sb-exercises/codealong/express-pg-intro-demo/VideoCode/pg-intro/node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (/home/tarvis/Documents/sb-exercises/codealong/express-pg-intro-demo/VideoCode/pg-intro/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (/home/tarvis/Documents/sb-exercises/codealong/express-pg-intro-demo/VideoCode/pg-intro/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
(node:6267) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6267) [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.

这是 db.js 文件

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

let DB_URI;

// If we're running in test "mode", use our test db
// Make sure to create both databases!
if (process.env.NODE_ENV === "test") {
  DB_URI = "postgresql:///usersdb_test";
} else {
  DB_URI = "postgresql:///usersdb";
}

let db = new Client({
  connectionString: DB_URI
});

db.connect();

module.exports = db;

我整天都在谷歌上搜索这个,大多数答案都涉及更改代码以添加用户凭据,但如果教师不需要这样做,那就没有必要了。我需要更改 postgres 中的某些设置吗?

很多答案涉及更改文件 pg_hba.conf

我在我的系统上找不到这个文件。我需要添加它吗?

(编辑:找到文件。我需要在这里更改什么?)

pg_hba.conf

标签: node.jspostgresql

解决方案


在我的手中,在与节点 pg 的连接 URI 中保留未指定主机 (:///) 与连接到 127.0.0.1 是一样的。这样做需要一个 md5 散列密码,如您的 pg_hba.conf 中指定的那样。

有很多方法可以解决这个问题。您可以更改连接 URI 以提供 URI 编码的密码,您可以更改 URI 以通过套接字 ( postgresql://%2Ftmp/userdbor postgresql://%2Fvar%2Frun%2Fpostgresql/userdb) 进行连接,您可以将 pg_hba 的 127.0.0.1/32 行更改为trustorident或其他不需要密码。


推荐阅读