postgres - Windows docker 容器中的密码身份验证失败

时间:2021-02-08 20:24:32

标签: javascript node.js postgresql docker

我正在尝试在 Windows 上使用 dockerized postgres + nodejs connction 做一个简单的入门练习。我跑了:

$ docker run -d --name dev-postgres -e POSTGRES_PASSWORD=testpw  -p 5432:5432 postgres      
701682e86a66f7efb81af2ebb3a92d442c52742d2cd3d008de690bf1fa73d896

然后使用 pg 库中的以下代码段:

const { Client } = require("pg");
const connectionString = "postgresql://postgres:testpw@localhost:5432/postgres";

const client = new Client({
  connectionString,
});
client.connect();

client.query("SELECT $1::text as message", ["Hello world!"], (err, res) => {
  console.log(err ? err.stack : res.rows[0].message); // Hello World!
  client.end();
});

但它崩溃了:

(node:8764) UnhandledPromiseRejectionWarning: error: password authentication failed for user "postgres"
    at Parser.parseErrorMessage (C:\Users\MikeSolomon\devel\user-flow-processor\vidgen\node_modules\pg-protocol\dist\parser.js:278:15)
    at Parser.handlePacket (C:\Users\MikeSolomon\devel\user-flow-processor\vidgen\node_modules\pg-protocol\dist\parser.js:126:29)
    at Parser.parse (C:\Users\MikeSolomon\devel\user-flow-processor\vidgen\node_modules\pg-protocol\dist\parser.js:39:38)
    at Socket.<anonymous> (C:\Users\MikeSolomon\devel\user-flow-processor\vidgen\node_modules\pg-protocol\dist\index.js:10:42)
    at Socket.emit (events.js:310:20)
    at addChunk (_stream_readable.js:286:12)
    at readableAddChunk (_stream_readable.js:268:9)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
(node:8764) 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:8764) [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.
Error: Connection terminated unexpectedly
    at Connection.<anonymous> (C:\Users\MikeSolomon\devel\user-flow-processor\vidgen\node_modules\pg\lib\client.js:132:73)
    at Object.onceWrapper (events.js:416:28)
    at Connection.emit (events.js:310:20)
    at Socket.<anonymous> (C:\Users\MikeSolomon\devel\user-flow-processor\vidgen\node_modules\pg\lib\connection.js:58:12)
    at Socket.emit (events.js:310:20)
    at TCP.<anonymous> (net.js:672:12)

我尝试了几种不同的方法来使配置正常工作,但还没有成功。任何人都可以发现我做错了什么吗?这是 Windows 的问题吗?我使用 exec bash 检查了 docker 容器,并且可以使用 psql 在容器内没有任何问题。感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

明智之举 - 事实证明,在 Windows 上,如果您有另一个 postgres 实例在 5432 上运行,则会以这种方式失败。我的解决方案是简单地停止该实例。不幸的是,由于它是在系统启动时启动的后台进程,所以我花了一段时间才弄清楚它在那里。