Connecting with SSL

I'm using a db with sslmode=require which I thought needed encryption, but doesn't actually check the certificate. I keep running into an error:

Error: self-signed certificate in certificate chain

Here's the code I use to create my connection singleton.

import { drizzle } from "drizzle-orm/node-postgres";
import { Client } from "pg";
import * as schema from "./schema.js";
import "dotenv/config";
import fs from "fs";


type DbMode = "dev" | "prod";

const url = process.env.DB_URL || "";
const mode: DbMode = url.includes("localhost") ? "dev" : "prod";
const ssl =
  mode == "dev"
    ? undefined
    : {
        rejectUnauthorized: false,
        ca: fs.readFileSync("./build/db/cert.pem").toString(),
      };

const client = new Client({
  connectionString: url,
  ssl: ssl,
});

const db = drizzle(client);

export { db, schema, mode, client, url, ssl };


Am I missing something here? FWIW, the error doesn't throw in this file because I don't actually call await client.connect() because can't have top level awaits. Ideally I can keep this as a singleton instead of exporting a function to connect to it. Thxx!

The picture below shows the docs from node-postgres. It says not to use both the sslmode and the ssl config, but the error persists no in both of these
Screenshot_2024-07-17_at_10.07.26_AM.png
Was this page helpful?