如何在 Ubuntu 16.04 上使用 node.js 在本地连接到 postgresql?

How to connect to postgresql locally with node.js on Ubuntu 16.04?

我正在使用 Digitalocean droplet 并使用本教程 https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04 进行设置。当我使用我的用户连接时,postgres 在机器上运行。

当我尝试使用 node.js 连接到 postgres 数据库时,它不起作用。

var pg = require('pg');
var conString = "postgres://myuser:password@localhost:5432/dbname";

var client = new pg.Client(conString);
client.connect();

当我查看 node.js 错误日志时,它说

error: password authentication failed for user "myuser"
    at Connection.parseE (/usr/lib/node_modules/pg/lib/connection.js:572:11)
    at Connection.parseMessage (/usr/lib/node_modules/pg/lib/connection.js:396:$
    at Socket.<anonymous> (/usr/lib/node_modules/pg/lib/connection.js:132:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:547:20)

我不确定密码是什么意思。我输入的唯一密码是我用来在 Ubuntu 上创建 myuser 的密码,就像教程中指示的那样。我确定密码与我为 myuser 输入的密码相匹配。我也尝试将其留空,但这也不起作用。

但我假设连接有效,只有身份验证失败?新的 postgres 数据库是否有一些默认密码?

好的,我找到方法了

$ sudo -u myuser psql myuser
postgres=# \password myuser

然后它询问我的密码我输入了与连接字符串中相同的密码,现在它可以工作了,很奇怪。