postgresql NodeJS Postgres 错误 getaddrinfo ENOTFOUND
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34802333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJS Postgres error getaddrinfo ENOTFOUND
提问by arkhários
I use pg://user:pass@localhost:port/table
for connecting to my AWS database. When I use localhost, the app works fine, but when I try to connect the AWS server it falls apart.
我pg://user:pass@localhost:port/table
用于连接到我的 AWS 数据库。当我使用 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.
即使是一个简单的连接代码也会给我这个错误。数据库名称是人,它在端口 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();
回答by Kurotsuki
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. 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).
如果您确定您的连接字符串已经像 gnerkus 描述的那样格式正确,那么您需要检查的最后一件事就是您的密码。如果它包含非字母数字字符,也许这就是导致问题的原因。似乎是 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. Once I get rid of it (from my connection string and updating my database's password), it's working fine.
我的密码包含'+'
和'/'
(通过创建一个充满乱码的长 json 然后对其进行散列生成 base64 字符串来获得),我确实收到了与您相同的错误。一旦我摆脱了它(从我的连接字符串并更新我的数据库密码),它就可以正常工作。
Oh, and ... '='
is accepted though. Because it seems the problem is with url decoding process at the database side. 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). Take a look at this example.
哦,而且......'='
虽然被接受了。因为问题似乎出在数据库端的 url 解码过程中。当我发送时'+'
,我认为它被替换为' '
which 会导致密码错误。这'/'
导致了格式错误的 url,这是我们错误的根本原因(即未找到)。看看这个例子。
postgres://username:sdkadady88da8+8ahdajd/ashdi==@localhost/database
I'm sure you'll realize that there are extra '/'
which will cause wrong url break down. So, protocol:// user:pass@host / database
changed into protocol:// [malformed user:pass@host] / [malformed database name] / [some gibberish]
because of that extra '/'
.
我相信你会意识到有额外的'/'
会导致错误的 url 分解。所以,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. 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.
如果使用 JSF 访问它的同事可以编辑他们的连接字符串,我建议将密码更新为两者都接受的密码。如果他们不能,那么您需要创建另一个具有相同访问权限但可以从 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.
编辑:或者更好的是,根据这里的讨论,尝试对连接字符串的密码部分进行编码。他们说它有效。我没有费心去尝试,因为我已经更改了密码。由于您仍然遇到此问题,因此您可能想先尝试一下,然后再执行我上面的两个建议之一。
回答by gnerkus
The default port for a Postgres database connection is 5432. The Postgres database on AWS is probably running on that port. Also, the connection string should be in this format:
Postgres 数据库连接的默认端口是 5432。AWS 上的 Postgres 数据库可能正在该端口上运行。此外,连接字符串应采用以下格式:
var conString = "postgres://username:password@localhost/database";
var conString = "postgres://username:password@localhost/database";
as defined in the node-postgresdocumentation. You should update your connection string to:
如node-postgres文档中所定义。您应该将连接字符串更新为:
var conString = "postgres://someuser:pass@db-endpoint:5432/people";
回答by Shreyas
Make sure your URL does not contain http://
确保您的 URL 不包含 http://