Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Postgres error getaddrinfo ENOTFOUND

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.

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.

Error: 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();
like image 527
arkhários Avatar asked Jan 15 '16 00:01

arkhários


People also ask

How do I fix Getaddrinfo Enotfound error?

The error getaddrinfo ENOTFOUND localhost is caused by Webpack cannot found localhost address. To solve it, open the terminal: sudo nano /etc/hosts. Add following into the hosts file and save it.

What does Getaddrinfo Enotfound mean?

getaddrinfo ENOTFOUND means client was not able to connect to given address.


1 Answers

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).

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.

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.

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 '/'.

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.

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.

like image 174
Firanto Avatar answered Sep 27 '22 23:09

Firanto