Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

PostgreSQL not working with Node.js on Heroku

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:

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) {

I have tried various forms of this query, like this one:

var client = new pg.Client(process.env.DATABASE_URL);

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.

Problem courtesy of: Aerilys


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.

Solution courtesy of: Adam


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

PostgreSQL not working with Node.js on Heroku


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription