我可以从 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
})
没有错误,没有异常,数据库服务器没有响应!
为什么服务器没有响应?
问题是
Nodejs 12
我的 Mac 里有,但我NodeJS 14
的 Windows 里有,所以这是一个兼容性问题。由于我将我的 postgres (
pg
) 更新为8.0.3
或上级 (i.e. npm install pg
),现在它可以工作了!一个人帮助我在https://stackoverflow.com/questions/61611039/postgres-server-is-not-responding-to-a-nodejs-request?noredirect=1#comment108987882_61611039中意识到了这个问题。