Report this

What is the reason for this report?

Managed Database password authentication failed for user

Posted on September 9, 2020

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!

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.

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.

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.