简体   繁体   中英

nodejs error connecting to AWS RDS postgres database

I am trying to connect to my postgresql DB I am using the pg library, my code looks like this

const dbcreds = require("./dbconfig.json")
const { Pool } = require('pg')

pushToDB =  async function (shiftData) {
    const pool = await new Pool({
        host: dbcreds.dbhost,
        user: dbcreds.user, 
        password: dbcreds.dbpassword,
        database: dbcreds.dbname,
    })

    await pool.connect()
    await pool.query('INSERT into working (shift_id, client_id, organisational_unit, discipline, site, starts_at, ends_at, status) VALUES($1, $2, $3) returning id', ['shift id', 'client id', 'third'], function (err, result) {
        done();
        if (err) {
            return console.error('error running query', err);
        }
        res.send(result);
    });
}

Checking the logs on AWS shows me this after running my code, I think this may be due to AWS permission errors but I'm not sure exactly what I need to change:

2019-06-26 09:09:46 UTC:46-247-17-105.fluidata.co.uk(50780):robert@sstreamdb:[9627]:FATAL: password authentication failed for user "robert"
2019-06-26 09:09:46 UTC:46-247-17-105.fluidata.co.uk(50780):robert@sstreamdb:[9627]:DETAIL: Role "robert" does not exist.
Connection matched pg_hba.conf line 13: "host   all all 0.0.0.0/0   md5"
----------------------- END OF LOG ----------------------

RDS database username and password is not your AWS user name and password, they are different identities.

In this case, the error message is pretty clear : FATAL: password authentication failed for user "robert"

So either, the Postgres RDS username or password is incorrect. Check with the owner of the RDS database what correct credentials are. If you created the RDS database, be sure to create a Robert user at Postgres level. You can refer to this blog for more details https://aws.amazon.com/blogs/database/managing-postgresql-users-and-roles/ .

Finally, when you will get this working, we strongly discourage embedding database credentials in your application. I would suggest you to use AWS Secret Manager instead and rewrite your code to fetch the secret at runtime. You will find an example here : https://aws.amazon.com/blogs/security/rotate-amazon-rds-database-credentials-automatically-with-aws-secrets-manager/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM