Question

Managed Database password authentication failed for user

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?


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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.