Node.js 节点JS中的Lambda函数,使用visualstudio代码中的SAM插件并尝试连接到PG实例

Node.js 节点JS中的Lambda函数,使用visualstudio代码中的SAM插件并尝试连接到PG实例,node.js,docker,visual-studio-code,aws-lambda,aws-serverless,Node.js,Docker,Visual Studio Code,Aws Lambda,Aws Serverless,嗨,我正在使用sam插件在vscode中运行lambda代码。节点js 12.x中使用的运行时。本地postgresql实例中使用的数据库。 通过SAM,我无法连接到数据库。我的代码正在安装了docker desktop的windows 10笔记本电脑上运行 'use strict'; var pg = require('pg'); var connection = new pg.Client({ host : 'localhost', user : 'post

嗨,我正在使用sam插件在vscode中运行lambda代码。节点js 12.x中使用的运行时。本地postgresql实例中使用的数据库。 通过SAM,我无法连接到数据库。我的代码正在安装了docker desktop的windows 10笔记本电脑上运行

'use strict';
var pg      = require('pg');
var connection = new pg.Client({
  host     : 'localhost',
  user     : 'postgres',
  password : 'admin',
  database : 'logtest'
});

module.exports.hello = (event, context, callback) => {
  
  connection.connect();

  connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
    if (error) throw error;
    console.log('The solution is: ', results[0].solution);
    const response = {
      statusCode: 200,
      body: JSON.stringify({
        message: 'Go Serverless v1.0! Your function executed successfully!',
        solution: results[0].solution,
      }),
    };

    connection.end();

    callback(null, response);    
  });
};
我得到的错误是

2020-09-24T15:24:26.796Z        a127f8c8-1526-1fb9-fa7c-4fcbceda7929    ERROR   Unhandled Promise Rejection     {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: connect ECONNREFUSED 127.0.0.1:5432","reason":{"errorType":"Error","errorMessage":"connect ECONNREFUSED 127.0.0.1:5432","code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5432,"stack":["Error: connect ECONNREFUSED 127.0.0.1:5432","    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: connect ECONNREFUSED 127.0.0.1:5432","    at process.<anonymous> (/var/runtime/index.js:35:15)","    at process.emit (events.js:315:20)","    at processPromiseRejections (internal/process/promises.js:209:33)","    at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
Function 'HelloWorldFunction' timed out after 3 s
2020-09-24T15:24:26.796Z a127f8c8-1526-1fb9-fa7c-4fcbceda7929错误未处理承诺拒绝{“errorType”:“Runtime.UnhandledPromiserRejection”,“errorMessage”:“ERROR:connect-EconRefuse127.0.0.1:5432”,“reason:{”errorType:“ERROR”,“errorMessage:“connect-EconRefused127.0.0.1:5432”,“code:”EconRefused”,“errno:”“ECONNREFUSED”,“syscall:“connect”,“address:”127.0.0.1”,“port:”5432,“stack:”[“Error:connect ECONNREFUSED 127.0.0.1:5432”,“at TCPConnectWrap.afterConnect[as oncomplete](net.js:1141:16)”,“promiser:”:“{},“stack:”[“Runtime.unhandledpromiser:Error:connect ECONNREFUSED 127.0.0.0.1:5432”,“at process.(/var/Runtime/index.js:35:15)”,”at process.emit(events.js:315:20)”,“at processPromiseEjections(internal/process/promises.js:209:33)”,“at processTicksAndRejections(internal/process/task_queues.js:98:32)”]
函数“HelloWorldFunction”在3秒后超时
我尝试使用docker.for.win.localhost和host.docker.internal代替localhost,但没有成功