【问题标题】:nodejs error connecting to AWS RDS postgres databasenodejs 连接到 AWS RDS postgres 数据库时出错
【发布时间】:2019-11-08 05:37:59
【问题描述】:

我正在尝试连接到我的 postgresql DB 我正在使用pg 库,我的代码如下所示

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);
    });
}

在运行我的代码后检查 AWS 上的日志显示了这一点,我认为这可能是由于 AWS 权限错误,但我不确定我需要更改什么:

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 ----------------------

【问题讨论】:

    标签: node.js postgresql amazon-web-services amazon-rds


    【解决方案1】:

    RDS 数据库用户名和密码不是您的 AWS 用户名和密码,它们是不同的身份。

    在这种情况下,错误信息很清楚:FATAL: password authentication failed for user "robert"

    因此,Postgres RDS 用户名或密码不正确。请与 RDS 数据库的所有者确认正确的凭据是什么。如果您创建了 RDS 数据库,请务必在 Postgres 级别创建 Robert 用户。您可以参考此博客了解更多详情https://aws.amazon.com/blogs/database/managing-postgresql-users-and-roles/

    最后,当您可以使用此功能时,我们强烈建议您不要在您的应用程序中嵌入数据库凭据。我建议您改用 AWS Secret Manager 并重写您的代码以在运行时获取密钥。你会在这里找到一个例子:https://aws.amazon.com/blogs/security/rotate-amazon-rds-database-credentials-automatically-with-aws-secrets-manager/

    【讨论】:

    • 我已经使用 psql 连接到我的数据库,然后使用 create user robertgrant all privileges on database sstreamdb to robert 但我仍然遇到同样的错误
    • 你给用户“robert”分配了什么密码?此外,如果您连接到数据库以创建用户 robert,这意味着您确实 有一个用户名/密码来连接到它。如果您(暂时)尝试从您的代码中使用这些凭证怎么办?这行得通吗?
    • 连接到我的数据库我使用psql "host=dbcreds.dbhost port=5432 dbname=dbcreds.dbname user=dbcreds.user" 连接后我创建了用户robert 没有密码
    • 如果您的用户没有密码并且您将密码传递给连接password: dbcreds.dbpassword,,您将遇到问题。尝试为用户添加密码(无密码选项不安全)并使用您代码中的相同密码。
    • 我已经这样做了,但仍然收到错误我将如何指定我喜欢的用户
    猜你喜欢
    • 2019-05-27
    • 1970-01-01
    • 2021-04-21
    • 2019-09-28
    • 2021-11-15
    • 2021-09-05
    • 2020-03-14
    • 2020-04-09
    • 2014-12-24
    相关资源
    最近更新 更多