Aerilys
Aerilys

Reputation: 1639

PostgreSQL not working with Node.js on Heroku

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.

Upvotes: 1

Views: 4295

Answers (1)

Adam
Adam

Reputation: 1099

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.

Upvotes: 2

Related Questions