我在 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/