37

I am trying to connect to a PostgreSQL Database that I've set up in Heroku.

const { Sequelize, DataTypes, Model } = require("sequelize");

// DB Configuration
const sequelize = new Sequelize({
  database: "[wont'd show db]",
  username: "[won't show username]",
  password: "[won't show password]",
  host: "ec2-54-221-195-148.compute-1.amazonaws.com",
  port: 5432,
  dialect: "postgres",
  dialectOptions: {
    ssl: true,
  },
});

And this is what I am getting as the output:

SequelizeConnectionError: self signed certificate

James Z
  • 12,104
  • 10
  • 27
  • 43
Dmitriy Shin
  • 504
  • 1
  • 5
  • 8

5 Answers5

90

This is due to an (accidental) breaking change in node-postgres version 8 (see this GitHub issue).

The solution is to pass rejectUnauthorized: false to the sequelize connection parameters inside of dialectOptions>ssl, as described here by GitHub user jsanta:

const sequelize = new Sequelize({
  database: "xxxxx",
  username: "xxxxx",
  password: "xxxxx",
  host: "xxxxx",
  port: 5432,
  dialect: "postgres",
  dialectOptions: {
    ssl: {
      require: true,
      rejectUnauthorized: false // <<<<<<< YOU NEED THIS
    }
  },
});
CherryDT
  • 20,425
  • 2
  • 39
  • 63
4

in my case none of the above works, I use the connection string method to apply pg configurations, so I set the query param sslmode=no-verify and I got it works

example

postgres://myuser:mypassword@myhost:5432/mydatabasename?sslmode=no-verify
Anas
  • 501
  • 6
  • 12
  • Yes, in my case that's the case as well. Anyone knows why we have to do this? – Burak Karakuş Dec 28 '21 at 20:48
  • In my case this was the solution, my stack is kubernetes in digitalocean, managed database, and I created a database and a user that were not the default ones – agusgambina Mar 05 '22 at 17:07
0

It works for me (on sequelize config.json file):

    "dialect": "postgres",
    "dialectOptions": {
      "ssl": {
        "require": true,
        "rejectUnauthorized": false
      }
    }
0

This works for me, in the config.json file

  "development": {
    "username": "dummy",
    "password": "dummy",
    "database": "dummy",
    "host": "dummy",
    "dialect": "postgres",
    "dialectOptions":{
      "ssl": {
        "require": true,
        "rejectUnauthorized": false
      }
    }
  }
Jeremy Caney
  • 6,191
  • 35
  • 44
  • 70
  • Nice but this is the exact same solution I posted, just without explanation... Instead it refers to a file not everyone will even have. – CherryDT Jan 01 '22 at 10:52
-1

add the following in your code...

dbRDS=false
manikandan
  • 513
  • 2
  • 5
  • 15
  • Can you give a more complete example? It looks like a variable assignment but this alone won't have any effect... – CherryDT Jun 18 '21 at 23:42