NodeJS / psql: 出现错误 "connection terminated unexpectedly"

4

我正在尝试设置一个带有psql数据库的node js应用程序。当我运行node App.js时,我收到错误消息"Error: Connection terminated unexpectedly at Connection.con.once (/home/bismarck/Documents/node_modules/pg/lib/client.js:132:73)"

我确认我在应用程序中使用的凭据是正确的,我可以使用这些凭据查询mydb数据库。我还确认使用systemctl service start命令可以启动sql服务。以下是相关代码:

const config = {
 21         host: 'localhost',
 22         user: username,
 23         password: password,
 24         database: 'mydb',
 25         port: port,
 26 }
 27 
 28 const client = new Client(config);
 29 client.connect(err => {
 30         if (err) throw err;
 31         else {
 32                 getData();
 33         }
 34 });


现在留言已经太晚了。但是,我想知道您是否能够找出问题的原因并解决它? - Vijay Rathore
你找到解决方案了吗? - Prem popatia
1个回答

0

我也遇到了这个问题,在我的情况下是因为我尝试在生产环境中连接数据库。

所以,我向池中添加了ssl,像这样:

const pool = new Pool({
    connectionString: connectionString,
    ssl: {rejectUnauthorized: false},
});

希望它也能对你有所帮助...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接