Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

node-postgres fails locally with 'The server does not support SSL connections'

ErrorError: The server does not support SSL connections

Short 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.

  • Applies to node-postgres, PostgreSQL (current docs), Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 .
  2. 2Match what you see, then jump to its fix

    1. 1It 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. 2ssl 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. 3SHOW 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

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

Did this fix it?

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=disable

3Verify

Restart the app so it reads the new value, then run a query.

psql "$DATABASE_URL" -c "select 1"

Confirmed by raw.githubusercontent.com

Did this fix it?

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 = on

3Verify

After the restart, the setting reads on and a client with ssl enabled connects.

psql -c "SHOW ssl;"

Confirmed by postgresql.org

Did this fix it?

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

Next step

Want this handled for good?

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