When working with Node.js and PostgreSQL, you might encounter an error when trying to establish a secure connection to your database, especially when it’s hosted on cloud providers like Vercel. The error typically says something like:
Thank me by sharing on Twitter 🙏
error: connection is insecure (try using `sslmode=require`)In this post, we’ll walk through why this error happens, and how you can resolve it by correctly configuring the pg library for Node.js.
Error Description
The error occurs when attempting to connect to a PostgreSQL database without properly enabling SSL. The full error message looks like this:
This error is particularly common when using managed PostgreSQL databases that require secure connections, like those provided by Vercel, DigitalOcean, or Heroku.
Cause of the Error
The root cause of this error is a missing or improperly configured SSL option in your connection settings. PostgreSQL databases on cloud platforms often require SSL connections to ensure secure data transmission. If SSL is not explicitly enabled, the pg library defaults to an insecure connection, leading to this error.
The Founders: The Story of Paypal and the Entrepreneurs Who Shaped Silicon Valley
$19.68 (as of November 22, 2025 18:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)WOLFBOX MF50 Compressed Air Duster-110000RPM Super Power Electric Air Duster, 3-Gear Adjustable Mini Blower with Fast Charging, Dust Blower for Computer, Keyboard, House, Outdoor and Car
$31.99 (as of November 23, 2025 01:36 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)SanDisk 256GB Ultra microSDXC UHS-I Memory Card with Adapter - Up to 150MB/s, C10, U1, Full HD, A1, MicroSD Card - SDSQUAC-256G-GN6MA [New Version]
$25.99 (as of November 23, 2025 01:36 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Solution
The best way to resolve this error is by configuring the pg library to use SSL. Here’s how you can do that:
- Using the Connection String: If you’re connecting using a connection string, you can append
sslmode=requirelike this:
const { Pool } = require('pg');
const pool = new Pool({
connectionString: 'postgres://username:password@hostname:port/dbname?sslmode=require'
});
pool.connect();- Using the Configuration Object: A more explicit and preferred way is to set
ssl: truein your configuration:
const { Pool } = require('pg');
const pool = new Pool({
user: 'username',
host: 'hostname',
database: 'dbname',
password: 'password',
port: 5432,
ssl: true
});
pool.connect();By setting ssl: true, the library ensures a secure connection without needing any additional flags or options.
- Important Note: While setting
ssl: { rejectUnauthorized: false }is sometimes suggested, it should only be used for debugging purposes, as it bypasses certificate validation. For production use, always preferssl: truewithout overriding therejectUnauthorizedoption.
Verification
Once you’ve updated your connection settings, the error should disappear immediately when you use the pg connection pool. You should now be able to connect to your PostgreSQL database without any issues, and data transmission will be secure.
Conclusion
Ensuring a secure connection to your PostgreSQL database is crucial, especially when working with cloud-hosted services. By setting ssl: true in your pg configuration, you can quickly fix the “connection is insecure” error while keeping your connection secure.
If you found this post helpful or have additional insights, feel free to leave a comment below!
Tags/Keywords
- Node.js
- PostgreSQL
- pg library
- SSL
- Vercel


