node.js - Postgres 服务器没有响应 nodejs 请求

标签 node.js postgresql 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
 } 

我的测试如下:
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 
  })

没有错误,没有异常,数据库服务器没有响应!

为什么服务器没有响应?

最佳答案

我怀疑你有同样的问题,就像这个 other post .由于它不是 100% 重复,我将再次发布:

有一个known issue在 pg 模块和 NodeJS 14 中。

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

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

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

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

关于node.js - Postgres 服务器没有响应 nodejs 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61611039/

相关文章:

为 Postgres COPY 编写二进制格式的 Java 库?

node.js - 具有大量查询的 node-postgres

node.js - 将 postgres 与 nodejs 一起用于连接池

node.js - Webpack 不能使用__dirname?

node.js - 如何覆盖 npm 项目配置?

node.js - 异步函数永远不应该抛出异常吗?

javascript - 即使对于未定义为同步的方法,Node 的 Crypto 模块是否也会阻塞

postgresql - Postgres 将 Heroku 从 9.2.4 升级到 9.3

node.js - Nodejs 新错误(消息)和 Object.keys()

mysql - ISO 8601 日期使用什么数据类型?