【问题标题】:how to create a user in postgres to connect to nodejs?如何在 postgres 中创建用户以连接到 nodejs?
【发布时间】:2022-04-21 01:25:12
【问题描述】:

这是我第一次使用 postgres,我正在尝试将它与 node.js 一起使用。我在 Ubuntu 中安装了 postgres 并创建了用户 admin

sudo -i -u postgres
createuser --interactive

在我的节点代码中,我尝试连接到 postgres:

let { Client } = require('pg');
conn = new Client({host:'localhost', port:5432, database:'web-viewer', user: 'admin'});
conn.connect();

但我收到此错误

UnhandledPromiseRejectionWarning:错误:用户“admin”的密码验证失败

我的 pg_hba.conf 是

local   all             postgres                                peer

然后我改成

local   all             postgres                                md5

尝试为我的管理员用户添加密码,但是当我再次“createuser --interactive”时,我收到此错误

createuser:错误:无法连接到数据库模板 1:致命:用户“postgres”的密码验证失败

如何准备 postgreSQL 以连接到节点?

【问题讨论】:

    标签: javascript sql node.js postgresql


    【解决方案1】:

    您没有在new Client 定义中解析密码:

    在我的节点代码中,我尝试连接到 postgres:

    let { Client } = require('pg');
    conn = new Client({host:'localhost', port:5432, database:'web-viewer', user: 'admin'});
    conn.connect();
    

    使用您在创建用户期间保存的信息进行如下更改:

    let { Client } = require('pg');
    conn = new Client({
        host:'localhost', 
        port:5432, 
        database:'web-viewer', 
        user: 'admin',
        password: 'yourpassword'  
    });
    conn.connect();
    

    Follow the doc.

    顺便说一句,为避免使用敏感信息(您的密码),请考虑创建environment variables


    我知道这个帖子已经有 8 个月了,但你永远不知道。

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多