【问题标题】:Node app can't authenticate and connect postgres docker container节点应用程序无法验证和连接 postgres docker 容器
【发布时间】:2021-03-31 17:29:22
【问题描述】:

所以我试图让一个节点应用程序正确地连接到在 docker 中运行的 postgres db 并进行身份验证。我应该运行一个脚本,将数据添加到我添加到容器中的一个数据库中,但它一直抛出这个错误:

error: password authentication failed for user "myuser"
    at Parser.parseErrorMessage (.../node_modules/pg-protocol/dist/parser.js:278:15)
    at Parser.handlePacket (.../node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (.../node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (.../node_modules/pg-protocol/dist/index.js:10:42)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ migrate: `knex migrate:latest`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @ migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     .../.npm/_logs/2021-03-30T21_21_55_132Z-debug.log

根据 github 中的说明,我应该使用以下方法创建一个 postgres 容器:

docker run --name "postgres" -e POSTGRES_USER='{add-postgres-user}' -e POSTGRES_PASSWORD='{add-postgres-password}' -p 5432:5432 -d -t kartoza/postgis:12.4

我可以使用 psql 连接到它并使用我输入的密码和用户对其执行查询。但是,当我在 env 文件中输入相同的信息时,脚本读取它失败:

POSTGRES_HOST=172.17.0.2
POSTGRES_PORT=5432
POSTGRES_USER=myuser
POSRGRES_PASSWORD=password
POSRGRES_DATABASE=db_example

我尝试将 POSTGRES_HOST 更改为 localhost、容器名称、127.0.0.1 等内容,但无论我将其更改为什么都失败了。我确保按照说明输入了数据库。我不确定它是否与 docker 映像的主机绑定有关,但我认为它运行的 172 地址是正确的。我正在使用节点 14.16.0 运行 Ubuntu 20.04。

【问题讨论】:

  • 错字再次出现:POSTGRES_USER vs "POSR" GRES PASSWORD
  • 哇...主要的 derp 时刻
  • 他是对的。准确检查这两个 ENV VARS。你有错字。
  • 是的,解决了这个问题。

标签: node.js postgresql docker


【解决方案1】:

感谢Robert Kawecki,最后两个环境变量有错别字:

POSRGRES_PASSWORD=密码

POSRGRES_DATABASE=db_example

【讨论】:

    猜你喜欢
    • 2019-07-30
    • 1970-01-01
    • 2019-07-27
    • 2019-08-11
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    相关资源
    最近更新 更多