【发布时间】:2021-10-26 00:51:18
【问题描述】:
我有这个超级简单的 node-postgres 示例,但我无法运行:
const { Client } = require('pg')
const postgresOptions = {
connectionString: 'postgres://REDACTED:REDACTED@hattie.db.elephantsql.com/REDACTED',
connectionTimeoutMillis: 10000
}
const runDatabaseFunction = async function (functionToRun) {
// Connect db
const client = new Client(postgresOptions)
await client.connect() // Here’s where it times out after 10 seconds
// Run function
// const results = await functionToRun(client)
// Release db
await client.end()
return results
}
async function dbTest () {
const result = await runDatabaseFunction()
}
dbTest()
10000 毫秒后,它在 await client.connect() 处超时并出现此错误:
(node:80229) UnhandledPromiseRejectionWarning: Error: timeout expired
at Timeout._onTimeout (/REDACTED/node_modules/pg/lib/client.js:95:26)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
at emitUnhandledRejectionWarning (internal/process/promises.js:168:15)
at processPromiseRejections (internal/process/promises.js:247:11)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
为什么?!任何提示都是有帮助的。
- Heroku Postgres 和 ElephantSQL 存在相同的问题。
- 使用
psql或 Postico for Mac 连接工作正常,连接字符串相同。
【问题讨论】:
-
可能是防火墙问题。但是如果你使用 psql 连接到同一个 URL 会发生什么?
-
没有尝试过
psql,但 Postico for Mac 使用相同的连接字符串工作得很好。
标签: node.js postgresql node-postgres