postgresql 节点服务器无法连接到 postgres 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21740560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:18:44  来源:igfitidea点击:

node server can't connect to postgres db

node.jspostgresqlnode-postgres

提问by Dani

I recently switched from MySQL to postgres as my database for an node.js project. While I'm able to reach my remote postgres database from my local pgAdmin III (OSX) client, so far I've been unable to connect to my database through node.js. I'm sure that the credentials I entered for pgAdmin and my node.js were exactly the same. The other thing I've tried was setting my local ipadress to trust in stead of md5 in the pg_hba.conf at my database server. Is there anything I did wrong? My favourite search engine came up with some worrying hits about resetting my local os. I just used the example from the github repo doc of node-postgres:

我最近从 MySQL 切换到 postgres 作为 node.js 项目的数据库。虽然我可以从本地 pgAdmin III (OSX) 客户端访问我的远程 postgres 数据库,但到目前为止我无法通过 node.js 连接到我的数据库。我确信我为 pgAdmin 和 node.js 输入的凭据完全相同。我尝试过的另一件事是在我的数据库服务器的 pg_hba.conf 中将我的本地 ipadress 设置为信任而不是 md5。我做错了什么吗?我最喜欢的搜索引擎在重置我的本地操作系统时遇到了一些令人担忧的问题。我只是使用了 node-postgres 的 github repo 文档中的示例:

var pg = require('pg');

var conString = "postgres://myusername:mypassword@hostname:5432/dbname";

var client = new pg.Client(conString);
client.connect(function(err) {
  if(err) {
    return console.error('could not connect to postgres', err);
  }
  client.query('SELECT NOW() AS "theTime"', function(err, result) {
    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].theTime);
    client.end();
  });
});

And these are the errors I get every time I try to start my server:

这些是我每次尝试启动服务器时遇到的错误:

could not connect to postgres { [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

Help would be greatly appreciated

帮助将不胜感激

回答by Dani

It appears that node-postgresdoesn't accept a hashtag in your password. After removing the hashtag I was able to connect without a problem. Wouldn't have thought of that and it didn't strike me as a problem since pgAdmin accepted it.

似乎node-postgres不接受密码中的主题标签。删除主题标签后,我可以毫无问题地进行连接。不会想到这一点,自从 pgAdmin 接受它以来,它并没有给我带来问题。

The best solution would be to use encodeURIComponentto encode your password string. This would allow for hashtags to be used in the URL.

最好的解决方案是使用encodeURIComponent对密码字符串进行编码。这将允许在 URL 中使用主题标签。

回答by matthew.peters

The interface in node.js that I used can be found here https://github.com/brianc/node-postgres

我使用的 node.js 中的接口可以在这里找到https://github.com/brianc/node-postgres

var pg = require('pg');
var conString = "postgres://YourUserName:YourPassword@localhost:5432/YourDatabase";

try changing pg:// to postgres://

尝试将 pg:// 更改为 postgres://

This was scraped from another stackoverflow article: How to make connection to Postgres via Node.js

这是从另一篇 stackoverflow 文章中摘取的: How to make connection to Postgres via Node.js