问题
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.
I did NOT need to change pg in the dependencies. I left it at "pg": "4.x".
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