使用node-orm使node.js应用程序连接到PostgreSQL的问题(在osx 10.6.8下)

Pre*_*hev 6 macos node.js express

我试图在osx(10.6.8)下运行一个小nodejs应用程序,并连接到我的localhost Postgres数据库.我正在使用node-orm(https://github.com/dresende/node-orm2)以及Postgres绑定(https://github.com/brianc/node-postgres).

出于某种原因,当我使用orm.connect并将连接url作为字符串提供时,它会失败:

Error: getaddrinfo ENOENT
    at errnoException (dns.js:31:11)
    at Object.onanswer [as oncomplete] (dns.js:123:16)
Run Code Online (Sandbox Code Playgroud)

如果我用127.0.0.1更改localhost.它也会因超时异常而死亡.

使用vanilla postgress绑定连接到DB,使用相同的连接字符串是成功的,我甚至设法运行一些测试查询,得到结果等.

我尝试使用vanilla postgress api手动设置数据库客户端,将其传递给orm使用orm.use.这设法成功连接到数据库,但每次我尝试执行查询时,都没有任何反应.它不会产生错误,但会在QueryQueue中陷入困境,并且不会调用成功回调.我检查了数据库日志,似乎它还没有检测到查询.

我该怎么办?我有一点困惑

Mic*_*iot 8

我在飞机上遇到过这个问题.我只是Error: getaddrinfo ENOENT在没有连接到网络时收到,即使我在本地运行数据库服务器.这是我做的修复它:

而不是使用localhost,使用127.0.0.1.

所以你的连接字符串会改变

 postgresql://user:pass@localhost:5432/db_name
Run Code Online (Sandbox Code Playgroud)

postgresql://user:pass@127.0.0.1:5432/db_name
Run Code Online (Sandbox Code Playgroud)


ran*_*ess 1

无法从问题中找出真正的问题。看起来您的连接字符串是错误的。但这里有一个与https://github.com/brianc/node-postgres一起使用的代码块

`

var application_root = __dirname,
        pg = require('pg');

    // database

    var conString = "postgres://username:password@172.28.6.250:5432/db_name";

    function processReqResSql(req, res, sql) {
        pg.connect(conString, function (err, client) {
            if (err) {
                res.writeHead(404, {
                    'Content-Type': 'application/json'
                });
                res.write('{couldnt connect to db}');
            } else {


             var query = client.query(sql);
                /
                var dbResponse ="";
                query.on('row', function(row) {
                 console.log(row);

                  dbResponse = dbResponse+JSON.stringify(row);
                 });

                query.on('end', function() { 
                  client.end();
                   res.writeHead(200, {"Content-Type": "application/json"});
                   res.write("{ results:{"+dbResponse+"}");

                   res.end();
             });

            }
Run Code Online (Sandbox Code Playgroud)