Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

NestJS cannot open an SSL connection to a managed Postgres database

ErrorDEPTH_ZERO_SELF_SIGNED_CERT

Short answer

TypeORM passes its ssl option to node-postgres, which hands it to Node's TLS layer. Two things usually break the connection: the server only accepts SSL and your config sends none, or the server's certificate is not trusted (self-signed or private CA). Add an ssl object with the provider's CA certificate. Do not leave sslmode in the URL if you also set ssl.

  • Applies to NestJS 11, TypeORM 0.3, pg 8, Node.js 22, PostgreSQL 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact TLS error code from a plain Node script that uses the same connection string. The code tells you whether the problem is trust or a missing SSL setting.

    node -e "const {Client}=require('pg');const c=new Client({connectionString:process.env.DATABASE_URL});c.connect().then(()=>{console.log('connected');return c.end()}).catch(e=>console.log(e.code, e.message))"
  2. 2Match what you see, then jump to its fix

    1. 1DEPTH_ZERO_SELF_SIGNED_CERT or SELF_SIGNED_CERT_IN_CHAINThe server certificate is self-signed or signed by a private CAFix
    2. 2The same certificate error appears with the code unchanged, even after you tried an ssl settingVerification is switched off only as a throwaway workaroundFix
    3. 3You set ssl: { ca } in the config, but the CA is ignored and the same error stayssslmode in the connection string replaces your ssl objectFix
    4. 4The connection is rejected although host, user and password are right, and the psql command below with sslmode=require worksThe server only accepts SSL connections and the client sends noneFix

What does the error look like?

  • DEPTH_ZERO_SELF_SIGNED_CERT
  • SELF_SIGNED_CERT_IN_CHAIN

Cause 1 of 4

The server certificate is self-signed or signed by a private CA

1Check

Node verifies the server certificate by default (rejectUnauthorized is true). If the certificate is not signed by a CA Node trusts, the connection ends with one of these two codes.

grep -rn "ssl" src/ --include=*.ts
ls certs/

2Fix

Download the CA certificate from your database provider and give it to TypeORM in the ssl object, so verification stays on.

// app.module.ts
import { readFileSync } from 'fs';

TypeOrmModule.forRoot({
  type: 'postgres',
  host: process.env.DB_HOST,
  port: 5432,
  username: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  ssl: { ca: readFileSync('certs/db-root.crt').toString() },
  autoLoadEntities: true,
})

3Verify

Start the app and confirm TypeORM connects without the certificate error.

npm run start:dev

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 4

Verification is switched off only as a throwaway workaround

1Check

Node.js documents rejectUnauthorized: false as disabling verification, which exposes the connection to man-in-the-middle attacks. node-postgres shows it only in an example.

grep -rn "rejectUnauthorized" src/

2Fix

Use it only for a local test against a database you control, and replace it with the ca option before production.

ssl: { rejectUnauthorized: false },

3Verify

The same script from the first step prints connected.

node -e "const {Client}=require('pg');const c=new Client({connectionString:process.env.DATABASE_URL,ssl:{rejectUnauthorized:false}});c.connect().then(()=>{console.log('connected');return c.end()})"

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 4

sslmode in the connection string replaces your ssl object

1Check

node-postgres states that when a connection string contains sslmode, sslcert, sslkey or sslrootcert, the parsed result overwrites the ssl object.

echo $DATABASE_URL | grep -o 'ssl[a-z]*=[a-z-]*'

2Fix

Remove the ssl parameters from the URL and keep all SSL settings in the ssl object. Note that TypeORM's own url option is overridden by other options you set.

DATABASE_URL=postgres://user:pass@db.example.com:5432/app
// and keep ssl: { ca: ... } in the TypeORM options

3Verify

Restart the app; the CA you supplied is now used.

npm run start:dev

Confirmed by node-postgres.com

Did this fix it?

Cause 4 of 4

The server only accepts SSL connections and the client sends none

1Check

A pg_hba.conf record of type hostssl matches only SSL connections, and a hostnossl reject record refuses plain ones.

psql "host=db.example.com dbname=app user=app sslmode=require" -c 'select 1'

2Fix

Add the ssl object to the TypeORM options as in the first cause.

ssl: { ca: readFileSync('certs/db-root.crt').toString() },

3Verify

The psql command from the check succeeds, and so does the app.

npm run start:dev

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • Keep the CA certificate in the repository or a secret store, and load it by path from configuration.
  • Use one place for SSL settings: either the URL or the ssl object, not both.
  • Never ship rejectUnauthorized: false to production.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.