UnhandledPromiseRejectionWarning:错误:用户密码身份验证失败

Ami*_*ani 3 javascript postgresql node.js

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

createuser --interactive
Run Code Online (Sandbox Code Playgroud)

但它没有要求输入新密码,所以我认为密码设置为我电脑的 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()
}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

(node:7106) UnhandledPromiseRejectionWarning: error: password authentication failed for user "[username]"
Run Code Online (Sandbox Code Playgroud)

我该如何修复它?

小智 5

将 pg_hba.conf 上的访问方法修改为信任,为我做到了。

$ sudo nano /etc/postgresql/13/main/pg_hba.conf 
Run Code Online (Sandbox Code Playgroud)
# "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
Run Code Online (Sandbox Code Playgroud)

重新加载服务,然后再次尝试连接。

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