1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The real problem was the connection indeed. I have used the following method to do it instead: github.com/brianc/node-postgres/wiki/Client#constructor. It works now. Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.