首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过NodeJS连接远程数据库

问如何通过NodeJS连接远程数据库
EN

Stack Overflow用户
提问于 2019-11-08 21:29:18
回答 1查看 691关注 0票数 0

我正在尝试通过NodeJs连接到Heroku的数据库。

我尝试连接到localhost,它工作得很好。但是当我尝试远程访问数据库时,它给出了错误。

下面是运行良好的本地主机连接代码。

代码语言:javascript
复制
const conectionString='postgressql://postgres:1234@localhost:5433/Qfield'
const client= new Client({
    connectionString:conectionString
})

client.connect()

//client.query('select * from point',(err,res)=>{
//    console.log(err,res)
//    client.end()
//})

client.query('SELECT * FROM trees', function(err, res) {
    console.log(err,res)
    client.end()
    });

这是错误的连接,它是远程数据库。

代码语言:javascript
复制
const {Pool,Client}=require('pg')
const conectionString='postgressql://Username:Password@ec2-54-228-243-29.eu-west-1.compute.amazonaws.com:5432/database'
const client= new Client({
    connectionString:conectionString
})

client.connect()

//client.query('select * from point',(err,res)=>{
//    console.log(err,res)
//    client.end()
//})

client.query('SELECT * FROM trees', function(err, res) {
    console.log(err,res)
    client.end()
    });

这就是错误。

代码语言:javascript
复制
(node:14896) UnhandledPromiseRejectionWarning: error: no pg_hba.conf entry for host "193.140.225.88", user "vfoiltyjszbpav", database "dduigib0uc8ebt", SSL off
    at Connection.parseE (C:\Users\CBS Lab\Desktop\postgres_conn\node_modules\pg\lib\connection.js:604:11)
    at Connection.parseMessage (C:\Users\CBS Lab\Desktop\postgres_conn\node_modules\pg\lib\connection.js:401:19)
    at Socket.<anonymous> (C:\Users\CBS Lab\Desktop\postgres_conn\node_modules\pg\lib\connection.js:121:22)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
(node:14896) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14896) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Error: Connection terminated unexpectedly
    at Connection.con.once (C:\Users\CBS Lab\Desktop\postgres_conn\node_modules\pg\lib\client.js:252:9)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at Socket.<anonymous> (C:\Users\CBS Lab\Desktop\postgres_conn\node_modules\pg\lib\connection.js:131:10)
    at Socket.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

如何修复此错误?我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-08 21:36:24

将ssl=true添加到您的连接字符串以修复您收到的错误:

代码语言:javascript
复制
const conectionString='postgressql://Username:Password@ec2-54-228-243-29.eu-west-1.compute.amazonaws.com:5432/database?ssl=true'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58767522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档