27

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();
arkhários
  • 283
  • 1
  • 3
  • 7
  • Just for clarification are you trying to connect from the machine the database is running on? Or from a different host when you get that error? – enderv Jan 15 '16 at 04:24
  • you need to specify server address correctly. take reference of this link: http://stackoverflow.com/questions/23259697/error-getaddrinfo-enotfound-in-nodejs-for-get-call – Shubham Batra Jan 15 '16 at 07:19
  • I am running the app locally and trying to connect the aws db instance endpoint. – arkhários Jan 15 '16 at 08:51
  • Does this answer your question? [Node.js getaddrinfo ENOTFOUND](https://stackoverflow.com/questions/17690803/node-js-getaddrinfo-enotfound) – Henke Feb 02 '21 at 12:42

9 Answers9

30

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.

Community
  • 1
  • 1
Firanto
  • 609
  • 5
  • 22
  • 2
    Thanks for your input! I had an issue with my password which I fixed. It turns out that '#' is not accepted but '!' is. – arkhários Jan 26 '16 at 07:35
  • The # symbol in a password was also the problem for me. Thanks for the addition. – Andi-lo Aug 07 '17 at 12:37
  • 2
    @Andi-lo Yeah. Well.. Basically, all uri symbols are the problems. Either remove it, or url-encode the password part. – Firanto Aug 08 '17 at 18:40
10

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.

After this correction of DB_HOST, this issue was solved.

Wrong .env setting: DB_HOST=postgres

Correct .env setting: DB_HOST=localhost

Dharman
  • 30,962
  • 25
  • 85
  • 135
ferozpuri
  • 266
  • 2
  • 9
9

Encoding your password.

const userPasswordDatabase = `${encodeURIComponent(database.dbPassword)}@`;
Andmat7
  • 196
  • 2
  • 4
3

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:

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

as defined in the node-postgres documentation. You should update your connection string to:

var conString = "postgres://someuser:pass@db-endpoint:5432/people";
gnerkus
  • 11,357
  • 6
  • 47
  • 71
  • My colleague could access the database just fine using his JSF application. We have changed the port to 8080 for access. I refactored my code and it turns out that the connection information i provide is not being applied. I get the following error. `could not connect to postgres { [Error: getaddrinfo ENOTFOUND myapp myapp:5432] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'myapp', host: 'myapp', port: 5432 }` – arkhários Jan 16 '16 at 21:29
2

Make sure your URL does not contain http://

Shreyas
  • 1,927
  • 17
  • 34
2

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.

1

You might have written an "@" while passing the host to the pool class, remember it has to be like this "address string" not like this "@address string"

sudip modi
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 27 '22 at 06:59
1

Leaving here in case someone has the same issue as me

I put 127.0.0.1:5432 instead of 127.0.0.1

nopeless
  • 71
  • 3
0

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