【问题标题】:Problems making a node.js app connect to PostgreSQL, using node-orm (under osx 10.6.8)使用 node-orm(在 osx 10.6.8 下)使 node.js 应用程序连接到 PostgreSQL 时出现问题
【发布时间】:2018-01-10 14:51:00
【问题描述】:

我正在尝试让一个小型 nodejs 应用程序在 osx (10.6.8) 下运行,并连接到我的本地主机 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)

如果我将 localhost 更改为 127.0.0.1。它也会因超时异常而死。

使用 vanilla postgress 绑定连接到数据库,使用相同的连接字符串是成功的,我什至设法让一些测试查询运行,得到结果等。

我尝试使用 vanilla postgress api 手动设置 DB 客户端,然后使用 orm.use 将其传递给 orm。这设法成功连接到数据库,但每次我尝试执行查询时,什么都没有发生。它不会产生错误,但 sinply 会卡在 QueryQueue 中并且不会调用成功回调。我查了DB日志,好像也没有检测到查询。

我该怎么办?我有点迷茫

【问题讨论】:

  • 这似乎更像是 DNS 问题,而不是节点问题。指定与数据库的连接时是否使用 http://?看到这个问题:stackoverflow.com/questions/9580226/…
  • 这是我的连接地址:postgresql://user:pass@localhost:5432/db_name

标签: macos node.js express


【解决方案1】:

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

不要使用localhost,而是使用127.0.0.1

所以你的连接字符串将从

 postgresql://user:pass@localhost:5432/db_name

postgresql://user:pass@127.0.0.1:5432/db_name

【讨论】:

  • 我在厨房时遇到了同样的问题,wifi 不稳定。更改为 127.0.0.1 为我解决了这个问题!谢谢
【解决方案2】:

无法从问题中找出真正的问题。看起来您的连接字符串是错误的。 但这是一个与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();
             });

            }

【讨论】:

    【解决方案3】:

    检查你的 postgre 服务器是否已经启动。 您可以通过给出以下命令来启动它:

    pg_ctl -D <db-name> -l logfile start
    

    【讨论】:

      【解决方案4】:

      在我的情况下,我忘记根据需要添加标题。 Content-Type = application/json

      如果这不是导致错误的情况,您可以将您的数据库配置替换为:

      var config = {
              host: 'localhost',
              database: 'dbname',
              user: 'postgres',
              password: 'secret',
              port: 5432
          };
      

      替换代码:

      var config = {
          host: '127.0.0.1',
          database: 'dbname',
          user: 'postgres',
          password: 'secret',
          port: 5432
      };
      

      并使用此配置连接数据库:

      const {
              Pool
          } = require('pg');
          const pool = new Pool(config);
      
          pool.connect((err, client, done) => {
      // Your code logic
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        • 2020-04-18
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        相关资源
        最近更新 更多