我正在尝试使用 node-postgres 连接到远程数据库。
我可以使用 psql 客户端连接,但在尝试运行它时出现错误 Connection terminated unexpectedly
(使用与 psql 客户端相同的连接字符串):
const { Pool, Client } = require('pg')
const connectionString = '...'
const pool = new Pool({
connectionString: connectionString,
})
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res)
pool.end()
})
const client = new Client({
connectionString: connectionString,
})
client.connect()
client.query('SELECT NOW()', (err, res) => {
console.log(err, res)
client.end()
})
我也一直在尝试连接 Sequelize ORM,但遇到了同样的错误。
@编辑
使用本机模式修复了使用 pg 和 sequelize 进行客户端查询的问题
const { Pool, Client } = require('pg').native
原文由 John Jeromin 发布,翻译遵循 CC BY-SA 4.0 许可协议
我开始遇到同样的问题,但只有长时间查询,我通过在 Pool 构造函数中设置 idleTimeoutMillis 找到了一个可能的解决方案,例如设置为 20000(默认值为 10000)
请参阅 https://node-postgres.com/api/pool#new-pool-config-object-