javascript - 未处理的 promise 拒绝警告 : error: password authentication failed for user

标签 javascript node.js postgresql

我在 中创建了一个用户使用此命令:

createuser --interactive

但它没有要求输入新密码,所以我认为密码设置为我计算机的 sudo 密码。但是,当我尝试连接到我的数据库时,使用以下代码:

async function connect() {
    let connection_string = "postgres://[username]:[password]@localhost:5432/[database]"
    let client = new pg.Client(connection_string)
    await client.connect()

    let query = await client.query("select * from Messages;")
    query.rows.forEach(row => {
        console.log(row)
    })
    await client.end()
}

我收到这个错误:

(node:7106) UnhandledPromiseRejectionWarning: error: password authentication failed for user "[username]"

我该如何解决?

最佳答案

将 pg_hba.conf 上的访问方法修改为信任,为我完成。

$ sudo nano /etc/postgresql/13/main/pg_hba.conf 
# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

重新加载服务,并尝试重新连接。

const DB_URI = "postgresql:///<database_here>";
let db = new Client({ connectionString: DB_URI });
db.connect();

关于javascript - 未处理的 promise 拒绝警告 : error: password authentication failed for user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946114/

相关文章:

javascript - 执行ajax函数直到另一个ajax调用成功

javascript - 具有 10k 数据点的数据集的 Highcharts 替代方案?

node.js - 如何在node.js + Express.js + mongodb应用程序上异步启动mocha测试

ruby-on-rails - 如何分组和限制计数

javascript - Mocha + react : navigator is not defined

javascript - 如何在 node.js 中将 derby.js 与 express.js 集成?

node.js - 在带有 Node.js 的 Linux 服务器上使用 Microsoft Access 数据库 .accdb

javascript - 带有 TypeScript 的函数名称的 ESlint 规则(驼峰式,首字母小写)

java - 按代码排序

postgresql - 从 Kubernetes Jumpbox 运行 Postgres CLI 客户端