【问题标题】:Error when connecting to postgresql on localhost with knex使用 knex 连接到本地主机上的 postgresql 时出错
【发布时间】:2017-02-02 21:00:52
【问题描述】:

我正在尝试使用 knex 连接到我的本地 postgres 数据库,但我不断收到此错误。

{ Error: connect ECONNREFUSED 127.0.0.1:5432
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 5432 }

// Here is how everything is set up:
const connection = require('./db/knexfile.js').development;
const knex = require('knex')(connection);
const app = express();
const server = app.listen(PORT, '127.0.0.1', 'localhost', () => console.log(`Listening on ${ PORT }`));

// Inside knexfile.js 
module.exports = {
  development: {
    client: 'pg',
    connection: {
      "user": "development",
      "password": "development",
      "database": "testdb",
      "host": "127.0.0.1",
      "port": 5432
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  }
};

附加信息: 我还在 localhost:3000 上通过 webpack 运行 React 前端,它通过 webpack 服务器上的代理设置将 http 请求发送到 8080,但这似乎工作正常。

我还在 localhost:9200 上运行了 elasticsearch,这似乎也在工作。

【问题讨论】:

    标签: node.js postgresql express localhost knex.js


    【解决方案1】:

    问题是我安装了不正确的 postgres 版本。我重新安装了 postgres 并且不再有问题。

    【讨论】:

      【解决方案2】:

      另一种更快的方法是使用 AF_UNIX 套接字连接到本地数据库。您必须向托管应用程序的 Linux 用户授予访问权限:

      development:
        connection: {
          host: '/var/run/postgresql', // On Debian it is this one, check your distro
          database: 'database_name'
        },
      

      经测量,这种访问速度比通过 AF_INET (TCP/IP) 套接字快 30%。

      这里有一篇关于那个的旧文章:Performance of UNIX sockets vs TCP sockets

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多