title: NodeJS and Express: "Error: self signed certificate"
body: <p>I'm a beginner in NodeJS, and I have a very simple Node/Express application that uses PostGreSQL as the database. My "db.js" file looks like this:</p>
  
  <pre><code>const { Pool } = require('pg');
  
  module.exports = new Pool({
      user: 'postgres',
      password: 'xxxx',
      host: 'localhost',
      port: 5432,
      database:'gymmanager'
  });
  </code></pre>
  
  <p>When I was only running locally, everything was working fine, so I decided do deploy the app to Heroku. In order to do so, I've created a .env file with my environment variables for development. Here is the .env file:</p>
  
  <pre><code>NODE_ENV=dev
  DB_USER='postgres'
  DB_PASS='xxxx'
  DB_HOST='localhost'
  DB_PORT=5432
  DB_DATABASE='gymmanager'
  </code></pre>
  
  <p>And I have changed my "db.js" file, that now looks like this:</p>
  
  <pre><code>if(process.env.NODE_ENV !== 'dev') {
  
      const { Client } = require('pg');
  
      const client = new Client({  
          connectionString: process.env.DATABASE_URL,
          ssl: true
      });
  
      client.connect();
      module.exports = client;
  
  } else {
  
      require('dotenv').config();
      const { Pool } = require('pg');
  
      module.exports = new Pool({
          user: process.env.DB_USER,
          password: process.env.DB_PASS,
          host: process.env.DB_HOST,
          port: process.env.DB_PORT,
          database: process.env.DB_DATABASE,
          rejectUnauthorized: false
      });    
  }
  </code></pre>
  
  <p>My app was deployed and is running smoothly on Heroku, but now, when I run 'npm start' locally, I get the following Error:</p>
  
  <pre><code>(node:12989) UnhandledPromiseRejectionWarning: Error: self signed certificate
      at TLSSocket.onConnectSecure (_tls_wrap.js:1491:34)
      at TLSSocket.emit (events.js:315:20)
      at TLSSocket._finishInit (_tls_wrap.js:933:8)
      at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:691:12)
  </code></pre>
  
  <p>I have tried several solutions...</p>
  
  <p><a href="https://stackoverflow.com/questions/10888610/ignore-invalid-self-signed-ssl-certificate-in-node-js-with-https-request">Ignore invalid self-signed ssl certificate in node.js with https.request?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/45088006/nodejs-error-self-signed-certificate-in-certificate-chain">nodejs - error self signed certificate in certificate chain</a></p>
  
  <p>But none of them seems to work.
  Can anyone help me?</p>
  
  <p>Thanks in advance. :)</p>