Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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 ! –  Aerilys Apr 26 '13 at 10:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.