node-postgres fails locally with 'The server does not support SSL connections'
Error: The server does not support SSL connectionsShort answer
The client asked for SSL and the server answered that it has none. PostgreSQL's ssl setting is off by default, so a local database refuses. Look at where the client turns SSL on: an ssl option copied from a hosting guide, or an sslmode in the connection string. Switch it off locally, or enable ssl on the server.
Fix it fast
1Find out which cause you have
Find every place the client switches SSL on: the ssl option in the pool config and any sslmode in the connection string or environment.
grep -rn "ssl\|sslmode" --include=*.js --include=*.ts --include=.env* --exclude-dir=node_modules .2Match what you see, then jump to its fix
- 1
It fails on your machine or in a Docker database, while the hosted database works with the same codeThe pool config sets ssl, but the local server has SSL turned offFix - 2
ssl is false or absent in the code, yet the error remains, and DATABASE_URL ends in ?sslmode=require or similarThe connection string carries an sslmode that turns SSL onFix - 3
SHOW ssl returns off on a database that must be reachable over SSL, such as a staging serverYou want encrypted connections, but the server has ssl offFix
- 1
What does the error look like?
Error: The server does not support SSL connections
Cause 1 of 3
The pool config sets ssl, but the local server has SSL turned off
1Check
node-postgres sends an SSL request whenever ssl is set. If the server replies that it does not do SSL, the client closes the socket and throws this error. PostgreSQL's ssl parameter defaults to off.
psql -h localhost -U postgres -c "SHOW ssl;"
grep -rn "rejectUnauthorized\|ssl:" --include=*.js --include=*.ts --exclude-dir=node_modules .2Fix
Make ssl depend on the environment. The hosting guide that you copied the option from needs it, the local server does not.
const isProduction = process.env.NODE_ENV === 'production';
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: isProduction ? { rejectUnauthorized: false } : false,
});3Verify
Start the app against the local database; the first query succeeds.
node -e "const {Pool}=require('pg');new Pool({connectionString:process.env.DATABASE_URL,ssl:false}).query('select 1').then(r=>{console.log(r.rows);process.exit()})"Confirmed by devcenter.heroku.com
Cause 2 of 3
The connection string carries an sslmode that turns SSL on
1Check
pg-connection-string maps sslmode=require, prefer, verify-ca and verify-full to an SSL connection, and only sslmode=disable switches it off. node-postgres also replaces the ssl object from your config when the string contains sslmode, so setting ssl: false next to such a string does not help.
echo "$DATABASE_URL"2Fix
Use sslmode=disable in the local connection string, or leave sslmode out and do not set ssl.
DATABASE_URL=postgres://user:password@localhost:5432/appdb?sslmode=disable3Verify
Restart the app so it reads the new value, then run a query.
psql "$DATABASE_URL" -c "select 1"Confirmed by raw.githubusercontent.com
Cause 3 of 3
You want encrypted connections, but the server has ssl off
1Check
SSL is enabled with ssl = on in postgresql.conf, and the server needs a certificate and a private key. They are read from server.crt and server.key in the data directory unless ssl_cert_file and ssl_key_file name others.
psql -c "SHOW ssl;"
psql -c "SHOW ssl_cert_file;"2Fix
Create a certificate and key, restrict the key's permissions, set ssl = on and restart PostgreSQL. The self-signed command below is for testing; production should use a certificate from a certificate authority.
openssl req -new -x509 -days 365 -nodes -text -out server.crt \
-keyout server.key -subj "/CN=dbhost.example.com"
chmod 0600 server.key
# postgresql.conf
ssl = on3Verify
After the restart, the setting reads on and a client with ssl enabled connects.
psql -c "SHOW ssl;"Confirmed by postgresql.org
How do I stop it coming back?
- Keep one ssl decision in one place in the code, driven by an environment variable, and document it for the team.
- Use a connection string per environment instead of editing the code for local work.
- For hosted databases that require SSL, supply the provider's CA certificate through the ssl option instead of switching verification off.
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
Databases
NestJS cannot open an SSL connection to a managed Postgres database
SELF_SIGNED_CERT_IN_CHAIN
TypeORM passes its ssl option to node-postgres, which hands it to Node's TLS layer.
4 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 fixDatabases
Strapi cannot reach PostgreSQL on 127.0.0.1:5432
Error: connect ECONNREFUSED 127.0.0.1:5432
ECONNREFUSED means nothing accepted the connection at that address.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fix