pg-promise self signed certificate error in Postgres

pg-promise self signed certificate error in Postgres
Reading Time: < 1 minute

PG-Promise is a popular Node.js library for interacting with PostgreSQL databases. Unfortunately, it does not always work with self-signed SSL certificates generated by Postgres. If you’re seeing errors related to self-signed certificates when using PG-Promise, here is how to fix the issue.

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

“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

 = {
      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

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

const db = pgp(config);

About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.

Join 22 other subscribers

Leave a Reply