Reputation: 343
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();
Upvotes: 33
Views: 117041
Reputation: 609
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.
Upvotes: 30
Reputation: 123
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.
Upvotes: 0
Reputation: 657
It's almost always the password, keep it a simple string with alphabets and numerics.
Upvotes: 0
Reputation: 101
Leaving here in case someone has the same issue as me
I put 127.0.0.1:5432
instead of 127.0.0.1
Upvotes: 3
Reputation: 11
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"
Upvotes: 1
Reputation: 386
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
Upvotes: 17
Reputation: 21
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.
Upvotes: 2
Reputation: 366
Encoding your password.
const userPasswordDatabase = `${encodeURIComponent(database.dbPassword)}@`;
Upvotes: 10
Reputation: 1
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.
Upvotes: 0
Reputation: 12037
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";
Upvotes: 4