Postgres 服务器未响应 nodejs 请求

Maf*_*Maf 3 postgresql node.js node-postgres

我可以从 pgAdmin4 访问远程 postgres 数据库,也可以使用 Mac 从 nodejs 访问。现在我使用相同的代码来访问 Windows 中的数据库。我的连接代码如下:

const { Client } = require('pg'); //Importing the Postgres package
const hosts= require('../hosts'); //Using the file containig all hosts 
const connectionData = { //Begin creating the connection settings object
   host: hosts.DBHost, //DB host   
   port: hosts.DBPort, //DB hosts port
   database: hosts.DB, //DB
   user: hosts.DBUser, //DB user
   password: hosts.DBPassword, //DB user password
 } 
Run Code Online (Sandbox Code Playgroud)

我的测试如下:

var client = new Client(connectionData); //New client instance using the above connection settings
client.connect(); //Open the connection to the database()  
sql = "select * from myTable";
client.query(sql) 
  .then(response => {
    console.log ({"data": response}); //This isn't shown 
  })
  .catch(err => { 
    console.log({"error": err}); //This isn't shown neither 
  })
Run Code Online (Sandbox Code Playgroud)

没有错误,没有例外,数据库服务器没有响应!

为什么服务器没有响应?

mad*_*low 5

我怀疑你有和其他帖子一样的问题。由于它不是 100% 重复,我将再次发布:

pg 模块和 NodeJS 14 中存在一个已知问题

建议的解决方案是确保您已pg>=8.0.3安装。

这可以通过更新pg依赖项来完成。

还要确保依赖于pg模块的任何其他库也是最新的并且具有最新pg版本。

如果由于任何原因无法做到这一点 - 使用 Node 12 也应该可行。