By Kalimetric
Hi,
I have been trying to connect to my managed database server through node. I am using pg-promise library with the following code which makes use of sslrootcert:
"use strict";
const fs = require('fs')
const pgPromise = require("pg-promise");
const {ConnectionString} = require('connection-string');
const path = require('path');
var options = {
// Initialization Options
};
let pgp = pgPromise(options);
const dotenv = require('dotenv');
dotenv.config();
const a = new ConnectionString('postgresql://username:password@server:port/mydb?sslmode=require');
var cert= fs.readFileSync(__dirname + '/certs/ca-certificate.crt', 'utf8')
a.setDefaults({
params: {
sslrootcert : cert
}
});
let dbpool = pgp(a);
module.exports = { dbpool };
However, when I try to connect, I am getting:
error: password authentication failed for user "username"
Now, I am definite I am using the correct user and password. I have added my user and database to “Users and Databases”.
I have added my home ip to trusted sources.
I am using the connection string provided in “Overview” “Connection details”
I am not seeing any connection attempts in “Recent Logs”
Is there anything else I am overlooking? Have I passed the SSL check?
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!
Okay, got to the bottom of this one. Here is the updated code for my db.js file:
"use strict";
const fs = require('fs')
const pgPromise = require("pg-promise");
const {ConnectionString} = require('connection-string');
const path = require('path');
var options = {
// Initialization Options
};
let pgp = pgPromise(options);
const dotenv = require('dotenv');
dotenv.config();
const a = new ConnectionString('postgresql://username:password@server:port/mydb?sslmode=require');
var cert = path.join(__dirname, 'ca-certificate.crt')
a.setDefaults({
params: {
sslrootcert : cert
}
});
var connstring = a.toString();
let dbpool = pgp(connstring);
module.exports = { dbpool };
//# sourceMappingURL=db.js.map
So, importantly I needed to change the pgp connection to take a string of the connection-string object, and I also needed to change my cert variable to just represent the location of my ca-certificate. I also moved my ca-certificate to my app root directory. Finally, I updated pg-promise through npm.
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.