By Jus N
Wormy
I had trouble connecting to digitalocean database in node js and was getting errors such as:
Error: self signed certificate in certificate chain.
or
error: no pg_hba.conf entry for host "x.x.x.x", user "------", database "------", SSL off
but then I added ca-certificate.crt file into my node.js app newly created ssl folder and wrote this
const pg = require('pg')
const fs = require('fs')
const pool = new pg.Pool({
user: "-----",
password: "--------",
host: "-----------",
port: 25060,
database: "----",
ssl: true,
ssl: {
ca: fs.readFileSync('../ssl/ca-certificate.crt'),
rejectUnauthorized: true,
}
module.exports = pool;
This line fixed all those errors and now I can connect to the database
ca: fs.readFileSync('../ssl/ca-certificate.crt'),
but I feel it’s not very common practice to have ca-certificate.crt file in my app folder so I would like to know how to handle this?
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!
Hi there @beuu,
I have not tested this with NodeJS, but I’ve done it with other frameworks and PostgreSQL. What you can do is to add the certificate at:
~/.postgresql/root.crt
Then make sure that the file has secure permissions:
- chmod 600 ~/.postgresql/root.crt
That way the connection to your PostgreSQL server would default to using that certificate.
Let me know how it goes! Regards, Bobby
The certificates should be put in a folder dedicated to certificates and key files. An example location would be /usr/local/ssl/crt/. All of your certificates need to be in the same folder. Save the changes to the file once you are finished.
+1 for this I am running a node app on App Platform and I’m unsure how to get it on the server. Other than putting it in my repo, which, as @beuu said, seems like a bad idea.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.