47

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: "[won't 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

CherryDT
  • 25,571
  • 5
  • 49
  • 74
Dmitriy Shin
  • 633
  • 1
  • 8
  • 9

5 Answers5

122

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, bypassing the SSL certificate check (which is okay when connecting to a trusted server over a secure connection such as on your local host or between your own servers in the same network):

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
  • 25,571
  • 5
  • 49
  • 74
9

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
  • 1,000
  • 11
  • 18
  • 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
  • It happens because you use `uri` parameter for defining the connection, not the `pg` object parameter. PostgreSQL accepts several values for the `sslmode` parameter explained here: https://www.postgresql.org/docs/current/libpq-ssl.html. It is used for defining our acceptance level for unverified certificates. – Luki B. Subekti Aug 02 '22 at 12:26
2

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
  • 7,102
  • 69
  • 48
  • 77
  • 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
  • 677
  • 2
  • 8
  • 19
  • 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