我是pg-promise 的作者;)这不是第一次被问到这个问题,所以我在这里给它一个详细的解释。
当你像这样实例化一个新的数据库对象时:
const db = pgp(connection);
...它所做的一切 - 创建对象,但它不尝试连接。该库建立在连接池之上,只有实际的查询方法从池中请求连接。
From the official documentation:
Objectdb代表数据库协议,具有惰性数据库连接,即只有实际的查询方法获取和释放连接。因此,您应该只为每个连接详细信息创建一个全局/共享 db 对象。
但是,您可以通过调用方法connect 来强制建立连接,如下所示。虽然这种方法不是用于链接查询的推荐方法(应该使用Tasks),但它通常可以方便地检查连接。
我从自己的帖子中复制了示例:https://github.com/vitaly-t/pg-promise/issues/81
以下是同时以两种方式执行此操作的示例,因此您可以选择您更喜欢的方法。
const initOptions = {
// global event notification;
error(error, e) {
if (e.cn) {
// A connection-related error;
//
// Connections are reported back with the password hashed,
// for safe errors logging, without exposing passwords.
console.log('CN:', e.cn);
console.log('EVENT:', error.message || error);
}
}
};
const pgp = require('pg-promise')(initOptions);
// using an invalid connection string:
const db = pgp('postgresql://userName:password@host:port/database');
db.connect()
.then(obj => {
// Can check the server version here (pg-promise v10.1.0+):
const serverVersion = obj.client.serverVersion;
obj.done(); // success, release the connection;
})
.catch(error => {
console.log('ERROR:', error.message || error);
});
输出:
CN: postgresql://userName:########@host:port/database EVENT: getaddrinfo ENOTFOUND host host:5432 ERROR: getaddrinfo ENOTFOUND host host:5432
库中的每个错误首先通过全局error 事件处理程序报告,然后才在相应的.catch 处理程序中报告错误。
更新
测试连接的现代方法 + 一步获取服务器版本:
// tests connection and returns Postgres server version,
// if successful; or else rejects with connection error:
async function testConnection() {
const c = await db.connect(); // try to connect
c.done(); // success, release connection
return c.client.serverVersion; // return server version
}
链接