节点服务器无法连接到postgres db [英] node server can't connect to postgres db

查看:118
本文介绍了节点服务器无法连接到postgres db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

将不胜感激

推荐答案

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

It appears that node-postgres doesn'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.

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

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

这篇关于节点服务器无法连接到postgres db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆