Trouble Accessing Remote Postgres DB on Heroku from Local Node.js Webapp

空扰寡人 提交于 2020-01-15 18:48:49

问题


I struggled today to get my local node.js app to reach out to the db on Heroku instead of settling for a parallel db on my machine. Thanks to a post I tested and then stopped expecting

process.env.DATABASE_URL

to provide the URL and replaced it with the actual URL from

heroku config

along the lines of

var connectionString = "postgres://thinga:thingb@ec2-23-21-119-36.compute-1.amazonaws.com:5432/thingc";.

But that didn't solve the problem completely. I found I also had to use

var pg = require('pg').native;

to force SSL. And in order to get that to work I had to rollback my pg module to

"pg": "2.x"

There must be a better way. Anyone?

P.S. I also set

NODE_ENV: development

but I don't know if that makes any difference.


回答1:


You helped me fix the same problem on my machine, but I didn't have to go as far as you did.

I had already changed process.env.DATABASE_URL to connectionString after declaring:

var connectionString = 'postgres://thinga:thingb@ec2-54-204-42-119.compute-1.amazonaws.com:thingc';

Adding .native to var pg = require('pg'); seems to have made the difference for me.

  1. I did NOT need to change pg in the dependencies. I left it at "pg": "4.x".

  2. I did not use this: NODE_ENV: development.

But thank you so much for your help.



来源:https://stackoverflow.com/questions/27263559/trouble-accessing-remote-postgres-db-on-heroku-from-local-node-js-webapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!