title: Postgres client not queryable through AWS lambda
body: <p>I'm trying to connect to postgres through nodejs code.
  The nodejs-code is shown below.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const pg = require("pg");
  
  
  let client;
  async function connect () {
    if (client) return client
    client = new pg.Client({
      user             : "xxx",
      password         : "xxx",
      host             : "xxx.xxx.xxx.xxx",
      port             : "xxxx",
      application_name : "AWS-Lambda",
      database         : "xxx"
    });
    await client.connect()
    return client;
  }
  
  async function execute (query, values) {
    if (!client) throw ('CLIENT NOT CONNECTED');
    return await client.query({ text: query, values }); 
  }
  
  async function close () {
    if (!client) return false
    await client.end();
    client = undefined;
    return true;
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>facing the below error sometimes, <strong>which is unable to reproduce</strong></p>
  
  <pre><code>Error: Client was closed and is not queryable
  at process.nextTick (/var/task/node_modules/pg/lib/client.js:535:25)
  at process._tickCallback (internal/process/next_tick.js:61:11)
  </code></pre>
  
  <p>I tried searching at all the places for this, but unable to find the correct solution for this.
  And unable to reproduce this to investigate this.</p>
  
  <p>Could anyone help me cracking this.</p>