NodeJS Postgres error getaddrinfo ENOTFOUND

后端 未结 4 468
鱼传尺愫
鱼传尺愫 2021-01-04 21:16

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

相关标签:
4条回答
  • 2021-01-04 21:48

    Make sure your URL does not contain http://

    0 讨论(0)
  • 2021-01-04 21:52

    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.

    0 讨论(0)
  • 2021-01-04 21:55

    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.

    0 讨论(0)
  • 2021-01-04 22:00

    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";
    
    0 讨论(0)
提交回复
热议问题