33

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();
4
  • 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? Commented Jan 15, 2016 at 4:24
  • you need to specify server address correctly. take reference of this link: stackoverflow.com/questions/23259697/… Commented Jan 15, 2016 at 7:19
  • I am running the app locally and trying to connect the aws db instance endpoint. Commented Jan 15, 2016 at 8:51
  • Does this answer your question? Node.js getaddrinfo ENOTFOUND Commented Feb 2, 2021 at 12:42

11 Answers 11

31

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 contains non alphanumeric characters, maybe that's what causes the issue.

It seems either the Node.js or the way javascript works is itself causing this (I'm not really sure since pg-admin can connect using my initial password just fine).

My password contained '+' and '/' (I acquired by creating a long json filled with gibberish and then hashed it resulting in a base64 string) and I was facing same error as you. Once I got rid of those special characters (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 on the database side. When I sent '+', I think it was replaced by ' ' which caused an incorrect password. And the '/' was causing a 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 a badly formatted url. 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 accessed it using JSF can edit their connection string, I suggest to update the password to one that can be accepted by both. If they can't, then you need to create another user/role with same access rights but a 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 changed my password. Since you still have this issue, you might want to try it first before doing one of my two suggestions above.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your input! I had an issue with my password which I fixed. It turns out that '#' is not accepted but '!' is.
The # symbol in a password was also the problem for me. Thanks for the addition.
@Andi-lo Yeah. Well.. Basically, all uri symbols are the problems. Either remove it, or url-encode the password part.
17

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

Comments

10

Encoding your password.

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

Comments

4

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";

1 Comment

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 }
3

Leaving here in case someone has the same issue as me

I put 127.0.0.1:5432 instead of 127.0.0.1

Comments

2

Make sure your URL does not contain http://

Comments

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.

Comments

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"

1 Comment

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

Comments

0

It's almost always the password, keep it a simple string with alphabets and numerics.

Comments

0

This might be a rare scenario, but in my case, the issue was that I had my conString (in my case named DATABASE_URL) set as an env variable in a .env file with double quotes around it, as such: DATABASE_URL="pg://someuser:pass@db-endpoint:8080/postgres"

This worked fine when testing locally, but was clearly tripping up my deployment platform (Netlify). The fix was to remove the double quotes in my Netlify env variable, and just have: DATABASE_URL=pg://someuser:pass@db-endpoint:8080/postgres

I received the DATABASE_URL in this format with double quotes when copying from my cloud hosted db at Cockroach Labs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.