How to Resolve “Connection is Insecure (try using sslmode=require)” Error

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 🙏

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

Solution

The best way to resolve this error is by configuring the pg library to use SSL. Here’s how you can do that:

  1. Using the Connection String: If you’re connecting using a connection string, you can append sslmode=require like this:
TypeScript
   const { Pool } = require('pg');

   const pool = new Pool({
     connectionString: 'postgres://username:password@hostname:port/dbname?sslmode=require'
   });

   pool.connect();
  1. Using the Configuration Object: A more explicit and preferred way is to set ssl: true in your configuration:
TypeScript
   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.

  1. 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 prefer ssl: true without overriding the rejectUnauthorized 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
Share this:

Leave a Reply