pg-promise self signed certificate error in Postgres


Posted on Aug 20, 2020


featured image

If you are using Node.js with some of these packages: pg-promise or pg probably you are facing this issue.

Error: self signed certificate

To fix this issue you have to use the package with the following recommendations

The issue occurrs when we try to use the module this way:

      
const pgp = require('pg-promise')();
const db = pgp('postgres://john:pass123@localhost:5432/products');
  

Based on https://github.com/vitaly-t/pg-promise/wiki/Connection-Syntax

"Please note that overriding defaults via pg.defaults does not work for all parameters"

So if you use defaults configs like this, it will throw the SSL error anyway

      
   pgp.pg.defaults.ssl = {
      rejectUnauthorized: false
  }
  

This is the right way to use it:

      
let ssl = null;
if (process.env.NODE_ENV === 'development') {
   ssl = {rejectUnauthorized: false};
}

const config = {
   host: 'localhost',
   port: 5432,
   database: 'my-database-name',
   user: 'user-name',
   password: 'user-password',
   max: 30, // use up to 30 connections
   ssl:ssl
};

// Or you can use it this way
const config = {
   connectionString: 'postgres://john:pass123@localhost:5432/products',
   max: 30,
   ssl:ssl
};

const db = pgp(config);
  

postgres pg dev db nodejs
Search
Side Widget
You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers!