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

标签 node.js linux postgresql database-connection ubuntu-16.04

Soo 我正在使用 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

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

关于node.js - 如何在Ubuntu 16.04上使用node.js本地连接到postgresql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44389222/

相关文章:

sql - 在 Postgresql 中加入 1 个表和 2 个不同的表

postgresql - 如何在 postgresql 中使用不同的 WHERE 语句按组进行两个单独的计数?

javascript - 有人可以用 async/await 向我解释以下内容吗?

javascript - 在 Javascript 中分配变量

c - forks和pipes实现linux编译器

linux - 我需要 grep 特定行数的数据,例如第 1 行、第 6 行

c - C中的bool数组与位数组

mysql - psql 查询中的点表示法 - 有什么区别?

javascript - mysql 命令的问题

node.js - 如何在 Node.js 中使用 fs.createReadStream 按单词(以空格分隔并返回)读取文件,例如 `/\s+/g`?