NestJS cannot open an SSL connection to a managed Postgres database
DEPTH_ZERO_SELF_SIGNED_CERTShort 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.
Fix it fast
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))"2Match what you see, then jump to its fix
- 1
DEPTH_ZERO_SELF_SIGNED_CERT or SELF_SIGNED_CERT_IN_CHAINThe server certificate is self-signed or signed by a private CAFix - 2
The same certificate error appears with the code unchanged, even after you tried an ssl settingVerification is switched off only as a throwaway workaroundFix - 3
You 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
The 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
- 1
What does the error look like?
DEPTH_ZERO_SELF_SIGNED_CERTSELF_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:devConfirmed by nodejs.org
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
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 options3Verify
Restart the app; the CA you supplied is now used.
npm run start:devConfirmed by node-postgres.com
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:devConfirmed by postgresql.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
Node.js gets 'no pg_hba.conf entry for host' from PostgreSQL
error: no pg_hba.conf entry for host
The server found no pg_hba.conf rule matching your connection type, address, database and user, so it denied access.
3 causes, each with a fixDatabases
NestJS connects to MySQL at ::1 and is refused
Error: connect ECONNREFUSED ::1:3306
::1 is the IPv6 loopback address.
2 causes, each with a fixDatabases
node-postgres fails locally with 'The server does not support SSL connections'
Error: The server does not support SSL connections
The client asked for SSL and the server answered that it has none.
3 causes, each with a fixDatabases
Prisma says it can't reach the database server, but the database is up
Please make sure your database server is running at `localhost`:`5432`.
P1001 is Prisma's catch-all for any failed connection, even a wrong password, so do not trust the label.
4 causes, each with a fixDatabases
Prisma P1010: the database user was denied access
no pg_hba.conf entry for host "...", user "...", database "...", no encryption
P1010 means the database server refused the connection for that user and database.
3 causes, each with a fixDatabases
Sequelize fails with 'Operation timeout' while waiting for a connection
ConnectionAcquireTimeoutError
This error means Sequelize waited the full acquire time and no connection in its pool became free.
5 causes, each with a fix