Question

Cannot connect with dev database due to SSL issue

I’m using DigitalOcean App Platform to host a small node.js app. For the time being, I decided to go with the dev database that happens to be PostgreSQL. I understand it’s not meant to be used in production, but does that mean the app cannot connect to it remotely?

I’m using the pg package to create the database client.

const db_config = {
  user: process.env.PGUSER,
  password: process.env.PGPASSWORD,
  host: process.env.PGHOST,
  database: process.env.PGDATABASE,
  port: process.env.PGPORT,
  ssl: true
}

const client = new Client(db_config);
client.connect();

However, when that code executes, it returns the following:

(node:1) UnhandledPromiseRejectionWarning: Error: self signed certificate in certificate chain
     at TLSSocket.onConnectSecure (_tls_wrap.js:1502:34)
     at TLSSocket.emit (events.js:314:20)
     at TLSSocket._finishInit (_tls_wrap.js:937:8)
     at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:711:12)
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I’m not sure how to get around that. I’m specifying the SSL option in the config. Can anyone help?

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

So I ended up figuring out how to solve this. Later after I submitted this ticket. I didn’t realize up until then that the dev database provides an CA cert. So once I did, all I had to do was to add one extra option in the config object, as such:

const client = new Client({
  user: process.env.PGUSER,
  password: process.env.PGPASSWORD,
  host: process.env.PGHOST,
  database: process.env.PGDATABASE,
  port: process.env.PGPORT,
  ssl: {
    rejectUnauthorized: false,
    ca: process.env.CACERT,
  }
});

With that, I was finally able to connect to the PostgreSQL dev database.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.