DEV Community

Dudko Development
Dudko Development

Posted on Originally published at tabellum.dudko.dev

"no pg_hba.conf entry for host ..., no encryption": what each sslmode actually does

The error looks like a firewall problem, or a permissions one:

FATAL:  no pg_hba.conf entry for host "203.0.113.7", user "app", database "app", no encryption
Enter fullscreen mode Exit fullscreen mode

Usually it's neither. Read the last two words. The server is telling you the connection arrived without TLS, and that it has no rule letting a plain-text connection in. Servers up to PostgreSQL 13 print SSL off in the same place; from 14 on it says no encryption (we started a 12, a 13, a 14 and a 16 to check).

Hosted Postgres made this common. AWS's documentation says rds.force_ssl defaults to on for RDS for PostgreSQL 15 and later, and the rejection it shows for a non-TLS connection is this same message, in its SSL off form. The original Stack Overflow question is past 600,000 views. On GitHub, 43 issues opened since August 1, 2026 quote the error together with "no encryption" (GitHub search, November 30, 2026).

So the fix is "connect with TLS". Which setting does that depends on the client, though, and the same sslmode=require means different things in psql and in Node.js. That difference is behind the second error people hit, the one right after they fix the first.

We built a PostgreSQL server that accepts TLS connections only, the way a managed service does, and tried every sslmode against it from psql (libpq) and from node-postgres. Then we switched TLS off on the server and ran the psql cases again.

The setup

  • Server: PostgreSQL 16.15, ssl = on, and a pg_hba.conf with hostssl lines for the user postgres and nothing else. There is no rule at all for a non-TLS TCP connection.
  • Certificate: signed by a private CA we created, valid for the names localhost and db.example.com. No IP address in it.
  • A second, unrelated CA plays "the wrong root certificate", which is what a client has when someone handed it the wrong file.
  • Each case records the exact error, or, if it connected, the TLS version from pg_stat_ssl for its own session.

Clients: psql 16.15 (libpq), and node-postgres (pg 8.23.1, pg-connection-string 2.14.1) on Node 22.23.5. We ran it twice on November 30, 2026 with identical results. The psql cases also ran against PostgreSQL 18.7 and 19.0 with their own psql, and every row came out the same. The script is measure.sh; it needs a PostgreSQL install and openssl, or just Docker.

psql and libpq

sslmode (and anything else set) against a TLS-only server
disable no pg_hba.conf entry ... no encryption
allow connected, TLS 1.3
prefer (the default) connected, TLS 1.3
require connected, TLS 1.3, certificate not checked
require + sslrootcert= the right CA connected
require + sslrootcert= the wrong CA SSL error: certificate verify failed
verify-ca, no root certificate file root certificate file "..." does not exist
verify-ca, right CA connected
verify-ca, wrong CA SSL error: certificate verify failed
verify-full, right CA, host=localhost connected
verify-full, right CA, host=127.0.0.1 server certificate for "localhost" (and 2 other names) does not match host name "127.0.0.1"
verify-full, host=db.example.com hostaddr=127.0.0.1 connected
verify-full, sslrootcert=system SSL error: certificate verify failed (expected: our CA is private)
require, as a user no hostssl line names no pg_hba.conf entry ... SSL encryption

What this tells you:

With libpq, "no encryption" means something asked for it. The default, prefer, tries TLS first, and even allow ends up on TLS once the server turns plain text away. If psql or another libpq-based tool shows this error, go looking for sslmode=disable: in the connection string, in the PGSSLMODE environment variable, or in a saved connection.

require encrypts, but it doesn't check who's on the other end. And if a root certificate file happens to be there, it quietly behaves like verify-ca. The libpq docs say that's kept "for backwards compatibility" and advise against relying on it. In practice it means the same require connects on your laptop and fails on a colleague's machine that has a stale ~/.postgresql/root.crt.

verify-full checks the host name you typed. Connect by IP and the name check fails, even with the right CA. Through an SSH tunnel, host is usually localhost, which fails for the same reason. libpq has a way out that few people know about: set host to the name on the certificate and hostaddr to the address you actually connect to. The name is checked against host; the packets go to hostaddr.

sslrootcert=system (new in PostgreSQL 16) uses the operating system's trusted roots. That works when a provider's certificate chains to a public CA. It fails for a private one, as in our test. RDS signs with its own CAs, so there you download AWS's bundle and point sslrootcert at the file.

With TLS switched off on the server (ssl = off, host lines), disable, allow and prefer connected without TLS, and every other mode failed with:

server does not support SSL, but SSL was required
Enter fullscreen mode Exit fullscreen mode

That's the opposite problem, and it's fixed on the server. No client setting adds TLS to a server that doesn't offer it.

node-postgres

connection setup against the same server
no sslmode, no ssl option (the default) no pg_hba.conf entry ... no encryption
?sslmode=disable no encryption
?sslmode=prefer UNABLE_TO_VERIFY_LEAF_SIGNATURE, plus a SECURITY WARNING
?sslmode=require UNABLE_TO_VERIFY_LEAF_SIGNATURE
?sslmode=verify-ca UNABLE_TO_VERIFY_LEAF_SIGNATURE
?sslmode=no-verify connected, TLS 1.3
?sslmode=require&uselibpqcompat=true connected, TLS 1.3
ssl: { rejectUnauthorized: false }, no sslmode in the URL connected, TLS 1.3
?sslmode=require and ssl: { rejectUnauthorized: false } UNABLE_TO_VERIFY_LEAF_SIGNATURE
ssl: { ca }, host localhost connected
ssl: { ca }, host 127.0.0.1 ERR_TLS_CERT_ALTNAME_INVALID
?sslmode=verify-full&sslrootcert=<file> connected

Three ways it differs from libpq.

The default is no TLS at all. Where libpq's default would have connected, node-postgres's default gets the no encryption rejection. Which is why the Node.js version of the question has its own 157,000 views.

prefer, require and verify-ca in the URL all mean verify-full. pg 8.23.1 says so itself, in a warning printed on the first such connection: "The SSL modes 'prefer', 'require', and 'verify-ca' are treated as aliases for 'verify-full'", and it adds that pg 9 will switch to libpq's meaning. Until then, a DATABASE_URL copied from a provider's dashboard with sslmode=require on the end really does verify the certificate, and if Node doesn't trust the provider's CA, it fails. We got unable to verify the first certificate because our server sends only its own certificate. A server that sends its CA along gives you self-signed certificate in certificate chain instead.

The URL wins over the code. With sslmode=require in the connection string, the ssl: { rejectUnauthorized: false } passed right next to it was ignored, and the connection still failed. A Postgres MCP server ran into exactly this on November 16, 2026.

What we'd do in Node: give the client the provider's CA, either as ssl: { ca } or as sslmode=verify-full&sslrootcert=<file>, and connect by the host name on the certificate. sslmode=no-verify, or rejectUnauthorized: false with no sslmode in the URL, gets you encryption without verification. That's what libpq's require gives you. Fine as a decision. Not great as something you ended up with by accident while chasing an error.

Reading the error

  • no pg_hba.conf entry ..., no encryption (or SSL off on 13 and older): the client didn't use TLS. Turn it on in the client. A new password or a firewall rule won't change anything.
  • no pg_hba.conf entry ..., SSL encryption: TLS worked, and there really is no rule for that user, database or address. Only whoever runs the server can add one. Our server printed exactly this for a user its hostssl lines didn't name.
  • server does not support SSL, but SSL was required: TLS is off on the server. Fix it there.
  • certificate verify failed, unable to verify the first certificate, self-signed certificate in certificate chain: TLS works, but the client doesn't trust the CA. Give it the provider's CA file.
  • does not match host name, ERR_TLS_CERT_ALTNAME_INVALID: the CA is fine, but you connected by a name or IP that isn't on the certificate. Use the certificate's name, or hostaddr with libpq.

In Tabellum

Tabellum, the database client this blog belongs to, has a TLS mode on its connection form. For a hosted database you pick the provider under Cloud preset, which "fills in the port, TLS mode and driver settings the provider needs" (user guide, §2.2). The same chapter uses the mode names prefer, require and verify-full. A connection that can't connect "shows one line saying what failed and what to check — a refused port, a timeout, a wrong password or a TLS problem", and Copy details copies the full message (§2.7). When it quotes the server, the last two words are the ones to read. SSH and WireGuard tunnels are built in (§2.9). Pasting a postgres:// connection string fills in the form and lists what it set and what it couldn't (§2.3).

We didn't run Tabellum against this server, and the guide doesn't say how its require treats certificates, so we won't either. Check it against the guide and your provider before relying on it.

None of this helps with a pg_hba.conf you don't control, a server with TLS switched off, or the TLS settings inside your own application's driver. Those get fixed on the server and in your code, with the tables above.

If an AI agent is going to use these connections too, Read-only Postgres for an AI agent: only the GRANT holds covers the next thing to get right on the same database. And if you landed here from a backup script against a managed server, pg_dump version mismatch, both ways covers the other error those scripts hit.

Reproduce it

measure.sh creates the two CAs and the certificate, starts a throwaway PostgreSQL cluster on port 55432 that accepts TLS only, and runs the psql cases. Point NODE_PG_DIR at a folder where you ran npm install pg and it also runs node-pg.mjs against the same server. Then it switches TLS off and runs the psql cases again. With only Docker, from the folder you saved measure.sh in:

docker run --rm -v "$PWD":/m:ro -e PGBIN=/usr/lib/postgresql/18/bin postgres:18 bash /m/measure.sh
Enter fullscreen mode Exit fullscreen mode

Our output: PostgreSQL 16.15 with node-postgres, 18.7, 19.0.

One local server and a private CA is the whole scope. We didn't connect to RDS or any other real provider, and poolers such as PgBouncer and client certificates weren't part of it.


Originally published at https://tabellum.dudko.dev/blog/postgres-no-encryption-sslmode.

Top comments (0)