'Self signed certificate' error during query the Heroku hosted Postgres database from the Node.js application 'Self signed certificate' error during query the Heroku hosted Postgres database from the Node.js application heroku heroku

'Self signed certificate' error during query the Heroku hosted Postgres database from the Node.js application


Check you pg config. It sounds like you are using pg 8 which deprecates implicit disabling of certificate verification (as you have in your config where ssl is set to true but no ssl configuration is provided). Specify rejectUnauthorized: true to require a valid CA or rejectUnauthorized: false to explicitly opt out of MITM protection.

You can do so where you set up your pg config as follows

const client = new Client({  connectionString: connectionString,  ssl: { rejectUnauthorized: false }})


Below is a variation of the accepted answer using Knex.js. Tested on Heroku.

const parse = require('pg-connection-string').parse;const pgconfig = parse('your-pg-connection-string');pgconfig.ssl = { rejectUnauthorized: false };const knex = Knex({  client: 'pg',  connection: pgconfig,});


If anyone is still seeing issues with this after appending the SSL object to the Client object and they are using a connection string. Make sure that you don't have an ssl parameter in the connection string. If you are working with Digital Ocean this parameter is included in the generated connection string.

This is how Digital Ocean formats their connection strings by default

postgres://USERNAME:PASSWORD@HOST:PORT/DB_NAME:25060/defaultdb?&sslmode=require