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

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.

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.

Related

Database stop sending data to node server after trying to deploy on heroku. I am just getting A pending promise

I developed a node app server that get data requested through a postgres (sequelize ORM) which send the data to my react nextjs app when requested.
A few days ago I tried to host the application on heroku, which is when all hell broke loose. the application stopped working. I was getting a socket hangup error. I followed the error stack trace until i think i pinpointed the error on when my server request data from my database. I realized there are no more data coming through. Therefore, I am left hanging with a pending promise. I rebuild my backend. I also went back to previous commit when it was working and the application is still not working. The front-end is working bc it is running on a different host but my backend is not receiving data from my db anymore. I have tried everything i can think of and read a bunch of article on stack overflow and Github but have not figure it out yet. Also, I can no longer seed my seed file for some reason (also return pending promise).
ANY HELP WILL BE REALLY APPRECIATED.
I figure out what was the problem. My configuration on the server side with my database was not working. Therefore, I was getting an unresolved promise which caused the application to just keep loading while waiting for the data. By changing my config file to this:
`
const Sequelize = require("sequelize");
let database = process.env.DATABASE_URL;
let sequelize = "";
process.env.DATABASE_URL
? (sequelize = new Sequelize(database))
: (sequelize = new Sequelize(database, "postgres", "", {
dialect: "postgres",
logging: false,
}));
`module.exports = sequelize;
and adding my postgres database manually on my terminal when running heroku pg:psql to see and create my data base. I was able to make the application work once deployed on Heroku.

Heroku Postgres add-on connection string for Nodejs app

I have a problem deploying a Nodejs app with a Postgresql database. The database comes from Heroku itself (Heroku Postgres add-on, hobby-dev). My app refused to connect to the database.
I found where the problem came from but I can't find a clean solution. And I think I could have misunderstood something (I'm new to Node and Heroku).
Heroku automatically gives me an environment variable DATABASE_CONFIG that includes the port:
postgres://username:password#hostname:port/databasename
Then, to connect with pg in my app, I use process.env.DATABASE_CONFIG as a connection string. I do something like:
const client = new Client({
connectionString: connectionString,
})
client.connect()
This fails to connect.
But if instead of using this environment variable, I cheat and change it, removing the port number from this connection string, it works.
I don't know why but the problem is that Heroku gives you this DATABASE_URL with the port included and you can't change it.
Did I do something wrong? Is there a clean solution to avoid that?
(because what I did is ugly as I hard-coded the DATABASE_CONFIG without the port directly in my code)
Thanks for your help!
First off, I would avoid using new Client() as this can lead to your connection being bottlenecked. Instead, use connection pooling. You can read a more indepth answer into why you want to do that here.
As for you direct issue, personally I have had trouble connecting to heroku postgres databases in the past, but here is a (basic) typical setup that works for me 99% of the time:
let pg = require('pg');
if (process.env.DATABASE_URL) {
pg.defaults.ssl = true;
}
// include an OR statement if you switch between a local dev db and
// a remote heroku environment
let connString = process.env.DATABASE_URL || 'postgresql://postgres:password#localhost:localpostgresport/yourlocaldbname';
const { Pool } = require('pg');
const pool = new Pool({
connectionString : connString
});
My first guess would be that it may have to do with ssl not being enabled. I know that has cause me problems in the past. Secondly, you want to make sure that you uses the process.env.DATABASE_URL, as environment variable should be set as the postgres connection string by Heroku.

How to use multiple db connection strings depending on the environment in Node.js - Express - Mongo?

During development, using Monk, in my app.js I define the db variable and make it accessible to the router as follows:
var db = monk('localhost:27017/dbname');
[...]
app.use(function(req,res,next){
req.db = db;
next();
});
Now since things seem to be working well, I would like to deploy my app to Heroku. For this purpose, I created an account on mLab to have a db to use in production.
I have set the environment variable MONGODB_URI as
heroku config:set MONGODB_URI=mongodb://user:password#server:port/dbname
and I think now I could use it now in this way:
var db = mongo.db(process.env.MONGOLAB_URI);
Now, I would like to keep my dev environment as it is and when I push to Heroku have instead the production DB. How can I do this?
Maybe it's not complicated but I don't have much experience with databases and production in general so I would appreciate some help.
Ps. Also I see that mLab states that sandbox dbs (the free plan) are not suitable for production. However, given also that my app doesn't make a heavy use of database (mostly just store some data and maybe in the future display something out of it), do you think it is OK if I use a sandbox db anyway? What problems could I encounter doing so?

Web-based MongoDB (Mongo-express) how to use middleware

I'm new at nodejs and I gues therefor I can't get to it.
I'm trying to install https://github.com/mongo-express/mongo-express as middleware (I got it to work as standalone app).
I did those three steps
var mongo_express = require('mongo-express/lib/middleware')
var mongo_express_config = require('./mongo_express_config')
app.use('/mongo_express', mongo_express(mongo_express_config))
And after that not sure how to reach it(how do I run it). I hoped for the port 8081 to be active but it's not.
Can anyone help me and explain how this supposed to work?

Correct Use of Mongoskin

I usually work with mongoskin because I like to be close to the database. Usually, I do a setup with a file like db.coffee, that contains just this:
mongo = require 'mongoskin'
# either local
module.exports = mongo.db 'mongodb://localhost/database'
# or remote
module.exports = mongo.db 'mongodb://<user>:<pass>#<host>:<port>/<db>?auto_reconnect=true'
Then I use it in my other sources:
db = require 'db'
users = db.collection 'users'
# Now use the collection in handlers and middleware
This seems to work perfectly fine when I am using a local mongo server, I've had an uptime for months and it never turned out to be a problem.
However, when I am using the remote second, I get problem if the server runs longer than just a few minutes - the connection to the mongodb seems lost, despite auto_reconnect. I guess this is because the localhost connection is never closed automatically.
However this led me to thinking if I am maybe using mongoskin in a wrong way, or if there's simply a bug with the auto_reconnect?
ensure mongoskin is using the 1.0.0 or higher driver

Resources