title: 'Self signed certificate' error during query the Heroku hosted Postgres database from the Node.js application
body: <p>My Node.js app is able to work with local Postgres database via <code>npm pg</code> module. 
  I can connect to the Heroku hosted Postgres database (free Hobby Dev plan) via command line with <code>heroku pg:psql</code> command as well. 
  <strong>But</strong> when my Node.js app is trying to query to Heroku hosted Postgres database I am receiving an <code>self signed certificate</code> error.</p>
  
  <p>Here is the output with <code>self signed certificate</code> error:</p>
  
  <pre><code>(node:2100) UnhandledPromiseRejectionWarning: Error: self signed certificate
      at TLSSocket.onConnectSecure (_tls_wrap.js:1051:34)
      at TLSSocket.emit (events.js:189:13)
      at TLSSocket._finishInit (_tls_wrap.js:633:8)
  (node:2100) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:2100) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  D:\MY\DEV\PROJECTS\AdsSubscribeBot\test.js:57
    if (err) throw err;
             ^
  
  Error: Connection terminated unexpectedly
      at Connection.con.once (D:\MY\DEV\PROJECTS\AdsSubscribeBot\node_modules\pg\lib\client.js:264:9)
      at Object.onceWrapper (events.js:277:13)
      at Connection.emit (events.js:189:13)
      at Socket.&lt;anonymous&gt; (D:\MY\DEV\PROJECTS\AdsSubscribeBot\node_modules\pg\lib\connection.js:76:10)
      at Socket.emit (events.js:194:15)
      at TCP._handle.close (net.js:597:12)
  </code></pre>
  
  <p>Simpliest way to reproduce this error is to try use the sample code to connecting in Node.js from Heroku devcenter:
  <a href="https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js" rel="noreferrer">https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js</a></p>
  
  <p>Here is the sample of the code that causes <code>self signed certificate</code> error:</p>
  
  <pre><code>const connectionString = 'postgres://USERNAME:PASSWORD@HOST:PORT/DB_NAME';
  
  const { Client } = require('pg');
  
  const client = new Client({
    connectionString: connectionString,
    ssl: true
  });
  
  client.connect();
  
  client.query('SELECT * FROM users;', (err, res) =&gt; {
    if (err) throw err;
    for (let row of res.rows) {
      console.log(JSON.stringify(row));
    }
    client.end();
  });
  </code></pre>
  
  <p>Maybe someone has faced the same issue and know the way how to solve it.</p>
  
  <p>Thanks in advance for any help.</p>