8

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

Help would be greatly appreciated

4

2 回答 2

27

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

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

于 2014-02-13T20:41:11.070 回答
0

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

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

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

这是从另一篇 stackoverflow 文章中抓取的: 如何通过 Node.js 建立与 Postgres 的连接

于 2014-02-13T01:26:19.417 回答