PostgreSQL not working with Node.js on Heroku - node.js

I'm trying to work on a node.js application using Heroku and a PostgreSQL database.
I have followed the tutorial on Heroku documentation:
https://devcenter.heroku.com/articles/nodejs#using-a-postgres-database
Dependencies are good, and my code is basically the following:
var pg = require('pg');
pg.connect(process.env.DATABASE_URL, function(err, client) {
var query = client.query('CREATE TABLE users (id bigint auto_increment not null, login varchar(250), constraint pk_users primary key (id) )');
query.on('row', function(row) {
console.log(JSON.stringify(row));
});
});
I have tried various forms of this query, like this one:
var client = new pg.Client(process.env.DATABASE_URL);
client.connect();
But I got each time this error in my heroku logs:
at Object.afterConnect [as oncomplete] (net.js:875:19)
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
If someone has already encountered this kind of problem, any help would be welcome.
Thank you.

I would suggest trying a console.log(process.env.DATABASE_URL); and then using that address to manually connect using a client on your local machine. I understand that the Heroku database servers are open for remote connection(but I have been wrong before).
Failing that you can get a heroku console and use the postgre client.
ECONNREFUSED is a socket error that pops up when you try to connect to an address that is not contactable/connectable/listening.
If process.env.DATABASE_URL does return the correct and connectible address then I would be doing console.log(pg); to ensure that object is what I would expect it to be.

Related

Mongoose is unable to connect with DB running on Kubernetes: ECONNREFUSED 127.0.0.1:27017

I have a MongoDB running in a Kubernetes Pod. The Pod is forwarded to my 27017 Port on localhost. Opening localhost:21017 returns It looks like you are trying to access MongoDB over HTTP on the native driver port.. Additionaly I can connect to my DB via DataGrip.
However, when I try to create a connection with node.js it fails with the said error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
Checking my running services under Task-Manager -> Services displays no MongoDB. However I assume that this is right since my DB is running under Kubernetes on a Remote Server. Right?
Can someone tell me what I'm doing wrong?
Here's my code:
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/testing')
.catch(error => {
console.log("Error with message: " + error.message)
})
Update:
The above code works, if it's being executed in VSCode. When I try to run it in WebStorm it somehow doesn't. If anyone has made the same experience and knows the cause for this I'd love to know.

Can't connect to mongoose Error: MongoError: connect ETIMEDOUT?

I have a running db on mlab, however I cannot connect. This is my code:
mongoose.connect('mongodb://myUsername:myPassword#ds161012.mlab.com:61012/gpbdatabase');
const db = mongoose.connection;
db.on('error', (e) => console.log(e))
.once('open', () => console.log('Successfully connected to database'))
I am sure I am using the right username and password, it's a database user I create on Users tab at mlab. And this is my complete error statement.
{ MongoError: failed to connect to server [ds161012.mlab.com:61012] on
first connect [MongoError: connect ETIMEDOUT 54.78.29.56:61012]
How can I fix it?
I always had these problems and it was down to mLab just being that slow and unreliable. Eventually made one for local use while developing.
ETIMEDOUT may occurred when your database not start.
Make sure your database is started.

Cannot connect to MongoDB via NodeJS - No primary found in replica set error

I am having difficulty connecting to MongoDB via NodeJS. The Mongo setup consists of a shared cluster.
The URL is below:
mongodb://dev.testserver.com:27017,dev.testserver.com:27018,dev.testserver.com:27019/ua?w=0&replicaSet=dev-testserver-com&readPreference=secondary&slaveOk=true
The code that I have is below:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url, function(err db) {
if(!err) {
console.log("We are connected");
}
db.close();
});
The error that I keep getting is "MongoError: no primary found in replicaset".
Can someone please help?
Looks like there is a problem in your server(s). You need to get on the server(s) and run rs.status() to find out the state of your replica set, and each server's logs to find out if and why one or more has stopped or become disconnected.

MongoError: failed to connect to server on first connect - Only when offline

I get this error when trying to connect with MongoClient.connect, but only when I'm offline. As soon as I'm online, with no change in code and not even restarting mongoDB, my app connects every time.
AssertionError: null == { MongoError: failed to connect to server [localhost:27017] on first connect
My server.js looks like this:
// server.js
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var operations = require('./operations');
// Connection URL
var url = 'mongodb://localhost:27017/myApp';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
...
If I just use > mongo in the terminal, it connects with no problem even when offline. Did I miss something in the documentation?
Sorry! Only after I posted did Mongoose can't connect without internet appear in the related questions sidebar - not that I'm using Mongoose, but the answer exactly solved my problem also.

Can't connect to mongo lab on arduino yun

I can't connect to a database with mongodb or mongoose on nodejs on my arduino yun.
Unable to connect to the mongoDB server. Error: { [MongoError: Authentication failed.]
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
code: 18,
errmsg: 'Authentication failed.' }
I get that error. The code works fine on my desktop computer.
//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');
//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://*******:********#address:23118/arduino';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
// do some work here with the database.
//Close connection
db.close();
}
});
I am trying to connect to a mongo lab database. I have not tried connecting to any other database to troubleshoot. This is because I cannot find a free trial version where I do not need a credit card.
EDIT:
I got an Object Rocket instance and it works perfectly on my arduino yun. However it is very expensive; $30 a month (currently on the trial). And I don't need speed or lots of data. So if anyone could figure out why it doesnt work on mlab that would be great.
I've experienced some authentication issues in the past and I've addressed them following the next steps:
1) Identify version numbers of both mongo server and mongoose
2) Identify what authentication mechanism does your server use and make sure that mongoose is trying to authenticate with the one your server is expecting. If you're using an old version, some authentication mechanism might not be supported, so you'd need to upgrade.
3) Identify where the user has been created. Check out Authentication Database and make sure that your client is authenticating using such database. It can be different than the database you store your data. Indeed, it uses to be admin. If so, take a look to the authSource option you can add to your connection string. More info here.
In addition to all of the above, I'd recommend you to use the the mongo client to verify your credentials are right and you can connect to the database.

Resources