【发布时间】:2020-03-04 23:10:35
【问题描述】:
我想从 Azure 函数访问 Azure Postgres DB。 下面是Azure函数的node.js源码。
module.exports = async function (context, req) {
try {
context.log('Start function!');
var pg = require('pg');
const config = {
host: 'taxiexample.postgres.database.azure.com',
user: 'postgres@taxiexample',
password: 'QscBjk10;',
database: 'taxi',
port: 5432,
ssl: false
};
var client = new pg.Client(config);
const query = 'insert into taxi values (\'2\');';
context.log(query);
client.connect();
var res = client.query(query);
await client.end();
context.log(res);
} catch (e) {
context.log(e);
} finally {
context.res = {
status: 200,
body: "ok"
};
}
};
记录没有插入,res对象返回如下错误:
2020-03-04T23:03:59.804 [Information] Promise {
<rejected> Error: Connection terminated
at Connection.<anonymous> (D:\home\site\wwwroot\HttpTrigger1\node_modules\pg\lib\client.js:254:9)
at Object.onceWrapper (events.js:312:28)
at Connection.emit (events.js:228:7)
at Socket.<anonymous> (D:\home\site\wwwroot\HttpTrigger1\node_modules\pg\lib\connection.js:78:10)
at Socket.emit (events.js:223:5)
at TCP.<anonymous> (net.js:664:12)
}
2020-03-04T23:03:59.811 [Information] Executed 'Functions.HttpTrigger1' (Succeeded, Id=944dfb12-095d-4a28-a41d-555474b2b0ee)
你能帮帮我吗?谢谢
【问题讨论】:
-
能否尝试使用
.catch()的方法获取详细的错误信息? -
我已经插入了 .catch() 子句,但是代码没有启动任何异常。
-
您能检查一下您数据库的防火墙吗?您是否在防火墙中打开了
Allow access to Azure services。另外,请问您是否启用了Enforce SSL connection(docs.microsoft.com/en-us/azure/postgresql/…)?如果您这样做了,请在您的代码中将ssl设置为true或禁用它。
标签: node.js postgresql azure azure-functions azure-postgresql