简体   繁体   English

NodeJS Postgres 错误 getaddrinfo ENOTFOUND

[英]NodeJS Postgres error getaddrinfo ENOTFOUND

I use pg://user:pass@localhost:port/table for connecting to my AWS database.我使用pg://user:pass@localhost:port/table连接到我的 AWS 数据库。 When I use localhost, the app works fine, but when I try to connect the AWS server it falls apart.当我使用 localhost 时,该应用程序运行良好,但当我尝试连接 AWS 服务器时,它就崩溃了。

Even a simple connection code gives me this error.即使是一个简单的连接代码也会给我这个错误。 The database name is people and it's running on port 8080 but in this error it's showing 5432 even if I declare the correct port number in the conString.数据库名称是 people,它在端口 8080 上运行,但在此错误中,即使我在 conString 中声明了正确的端口号,它也会显示 5432。

Error: getaddrinfo ENOTFOUND people people:5432 at errnoException (dns.js:26:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)错误:getaddrinfo ENOTFOUND people people:5432 at errnoException (dns.js:26:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)

This is my code so far:到目前为止,这是我的代码:

var pg = require("pg");

var conString = "pg://someuser:pass@db-endpoint:8080/postgres";
var client = new pg.Client(conString);
client.connect();

If you're sure your connection string is already well-formed like the one gnerkus described, the last thing you need to check is your password.如果您确定您的连接字符串已经像 gnerkus 所描述的那样格式正确,那么您需要检查的最后一件事是您的密码。 If it's contain non alphanumeric characters, maybe that's the one who cause the issue.如果它包含非字母数字字符,则可能是导致问题的原因。 It seems either the Node.js or the way javascript work itself causing this (I'm not really sure since pg-admin can connect using my initial password just fine).似乎是 Node.js 或 javascript 本身的工作方式导致了这种情况(我不太确定,因为 pg-admin 可以使用我的初始密码进行连接就好了)。

My password was contain '+' and '/' (acquired by creating a long json filled with gibberish and then hash it resulting base64 string) and I sure does receiving the same error like yours.我的密码包含'+''/' (通过创建一个充满乱码的长 json,然后对其进行哈希处理得到 base64 字符串),我确实收到了和你一样的错误。 Once I get rid of it (from my connection string and updating my database's password), it's working fine.一旦我摆脱它(从我的连接字符串和更新我的数据库的密码),它工作正常。

Oh, and ... '=' is accepted though.哦,而且 ... '='被接受了。 Because it seems the problem is with url decoding process at the database side.因为似乎问题出在数据库端的 url 解码过程上。 When I sent '+' , I think it replaced by ' ' which will cause incorrect password.当我发送'+'时,我认为它被' '替换,这会导致密码错误。 And the '/' was causing malformed url which is the root cause of our error (which says not found). '/'导致了格式错误的 url,这是我们错误的根本原因(表示未找到)。 Take a look at this example.看看这个例子。

postgres://username:sdkadady88da8+8ahdajd/ashdi==@localhost/database

I'm sure you'll realize that there are extra '/' which will cause wrong url break down.我相信你会意识到有额外'/'会导致错误的 url 崩溃。 So, protocol:// user:pass@host / database changed into protocol:// [malformed user:pass@host] / [malformed database name] / [some gibberish] because of that extra '/' .因此, protocol:// user:pass@host / database更改为protocol:// [malformed user:pass@host] / [malformed database name] / [some gibberish]因为那个额外'/'

If your colleague who access it using JSF can edit their connection string, I suggest to update the password to one which accepted by both.如果您使用 JSF 访问它的同事可以编辑他们的连接字符串,我建议将密码更新为双方都接受的密码。 If they can't, then you need to create another user/role with same access right but different password that can be used from Node.js.如果他们不能,那么您需要创建另一个具有相同访问权限但可以从 Node.js 使用的不同密码的用户/角色。

EDIT: Or better yet, according to discussion here , try encode the password part of your connection string.编辑:或者更好的是,根据这里的讨论,尝试对连接字符串的密码部分进行编码。 They say it works.他们说它有效。 I didn't bother to try it since I already change my password.因为我已经更改了密码,所以我没有费心去尝试。 Since you still has this issue, you might want to try it first before doing one of my two suggestions above.由于您仍然遇到此问题,因此您可能需要先尝试一下,然后再执行我上面的两个建议之一。

编码您的密码。

const userPasswordDatabase = `${encodeURIComponent(database.dbPassword)}@`;

In my scenario, I was making a database connection with the docker container, Where I was using the wrong DB_HOST and I had used the docker service name as a DB_HOST, instead of localhost.在我的场景中,我正在与 docker 容器建立数据库连接,其中我使用了错误的 DB_HOST,并且我使用了 docker 服务名称作为 DB_HOST,而不是 localhost。

After this correction of DB_HOST, this issue was solved.在更正 DB_HOST 之后,这个问题就解决了。

Wrong .env setting: DB_HOST=postgres .env 设置错误: DB_HOST=postgres

Correct .env setting: DB_HOST=localhost正确的 .env 设置: DB_HOST=localhost

The default port for a Postgres database connection is 5432. The Postgres database on AWS is probably running on that port. Postgres 数据库连接的默认端口是 5432。AWS 上的 Postgres 数据库可能正在该端口上运行。 Also, the connection string should be in this format:此外,连接字符串应采用以下格式:

var conString = "postgres://username:password@localhost/database"; var conString = "postgres://username:password@localhost/database";

as defined in the node-postgres documentation.node-postgres文档中定义的那样。 You should update your connection string to:您应该将连接字符串更新为:

var conString = "postgres://someuser:pass@db-endpoint:5432/people";

确保您的 URL 不包含http://

Not sure if this has been put out there, but it wouldn't take the $ in my password.不确定这是否已经放在那里,但它不会在我的密码中使用 $。 Removed it and it worked like a charm.删除它,它就像一个魅力。

If it helps anyone, I had this problem trying to connect to a postgres db on AWS from an API hosted on Heroku using the pg npm package.如果它对任何人有帮助,我在尝试使用 pg npm 包从 Heroku 上托管的 API 连接到 AWS 上的 postgres 数据库时遇到了这个问题。 I changed by PGHOST environmental variable from https://aws-sondayy-prod.caoqasdfsdftwur.us-east-2.rds.amazonaws.com to aws-sondayy-prod.caoqasdfsdftwur.us-east-2.rds.amazonaws.com (ie removed the https://) and it started working.我通过 PGHOST 环境变量从https://aws-sondayy-prod.caoqasdfsdftwur.us-east-2.rds.amazonaws.com更改为 aws-sondayy-prod.caoqasdfsdftwur.us-east-2.rds.amazonaws.com (即删除了 https://)并开始工作。

您可能在将主机传递给池类时写了一个“@”,记住它必须像这个“地址字符串”而不是这个“@地址字符串”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM