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 Microsoft Office 365 Bible: The Most Updated and Complete Guide to Excel, Word, PowerPoint, Outlook, OneNote, OneDrive, Teams, Access, and Publisher from Beginners to Advanced
$34.17 (as of January 22, 2025 11: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.)Start with Why: How Great Leaders Inspire Everyone to Take Action
$10.49 (as of January 22, 2025 11: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.)HP 910XL Black High-yield Ink Cartridge | Works with HP OfficeJet 8010, 8020 Series, HP OfficeJet Pro 8020, 8030 Series | Eligible for Instant Ink | 3YL65AN
$47.89 (as of January 22, 2025 11: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.)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=require
like 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: true
in 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: true
without overriding therejectUnauthorized
option.
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