<< Back to main

Node.js: Can't connect to Managed Database ('SELF_SIGNED_CERT_IN_CHAIN' error)

Tech stack

  • DigitalOcean Postgresql Pool
  • Node 12.x

Dependencies

  • Knex ^0.20.13
  • Next ^10.0.1
  • pg ^7.18.2

Problem

I created a new Managed Database with PostgreSQL at DigitalOcean and was trying to deploy it to Vercel but was having troubles when building the code receiving the following error:

20:51:43.836	> Build error occurred
20:51:43.837	Error: self signed certificate in certificate chain
20:51:43.838	at TLSSocket.onConnectSecure (_tls_wrap.js:1502:34)
20:51:43.838	at TLSSocket.emit (events.js:314:20)
20:51:43.838	at TLSSocket._finishInit (_tls_wrap.js:937:8)
20:51:43.838	at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:711:12) {
20:51:43.838	type: 'Error',
20:51:43.838	code: 'SELF_SIGNED_CERT_IN_CHAIN'
20:51:43.838	}
20:51:43.900	error Command failed with exit code 1.
20:51:43.900	info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
20:51:43.924	Error: Command "yarn run build" exited with 1

Solution

  1. Set up the knexfile to be:
export default {
  development: {...},

  production: {
    client: "pg",
    connection: {
      connectionString: process.env.DATABASE_URL,
      ssl: {
        rejectUnauthorized: false,
      },
    },
  },
};
  1. Change the DATABASE_URL from:
postgresql://user:password@host:port/database?sslmode=require&ssl=true

to:

postgresql://user:password@host:port/database