title: Express/sequelize app deployed to Heroku giving SequelizeConnectionRefusedError
body: <p>I have an express/sequelize app that works fine in development on my machine. I've tried to deploy it via Heroku.  I have a postgres database linked to my heroku app.  The build passes, but the app won't launch.  The error message from Heroku logs is:</p>
  
  <blockquote>
    <p>2016-12-03T11:26:06.614800+00:00 app[web.1]: Unhandled rejection  SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5432
    2016-12-03T11:26:06.614808+00:00 app[web.1]:     at
    /app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:98:20
    2016-12-03T11:26:06.614809+00:00 app[web.1]:     at
    Connection. (/app/node_modules/pg/lib/client.js:185:5)
    2016-12-03T11:26:06.614810+00:00 app[web.1]:     at emitOne
    (events.js:96:13) 2016-12-03T11:26:06.614811+00:00 app[web.1]:     at
    Connection.emit (events.js:188:7) 2016-12-03T11:26:06.614812+00:00
    app[web.1]:     at Socket.
    (/app/node_modules/pg/lib/connection.js:71:10)
    2016-12-03T11:26:06.614812+00:00 app[web.1]:     at emitOne
    (events.js:96:13) 2016-12-03T11:26:06.614813+00:00 app[web.1]:     at
    Socket.emit (events.js:188:7) 2016-12-03T11:26:06.614814+00:00
    app[web.1]:     at emitErrorNT (net.js:1276:8)
    2016-12-03T11:26:06.614814+00:00 app[web.1]:     at
            _combinedTickCallback (internal/process/next_tick.js:74:11) 2016-12-03T11:26:06.614815+00:00 app[web.1]:     at
    process._tickCallback (internal/process/next_tick.js:98:9)
    2016-12-03T11:26:06.630985+00:00 app[web.1]: [nodemon] clean exit -
    waiting for changes before restart 2016-12-03T11:27:01.296756+00:00
    heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind
    to $PORT within 60 seconds of launch 2016-12-03T11:27:01.296831+00:00
    heroku[web.1]: Stopping process with SIGKILL
    2016-12-03T11:27:01.454936+00:00 heroku[web.1]: State changed from
    starting to crashed</p>
  </blockquote>
  
  <p>I believe this is an issue with the database setup for the production environment.  I've spent a long time googling and now really need some help to nudge me in the right direction!  Thank you in advance.</p>
  
  <p><strong>app/server.js</strong></p>
  
  <pre><code>const express = require('express'),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    app = express();
  const router = require('./router');
  var models = require('./models');
  
  var port = process.env.PORT || 3000;
  
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());
  app.use(cors());
  router(app);
  app.set('view engine', 'ejs');
  
  app.use(express.static(__dirname + '/public'));
  
  
  models.sequelize.sync().then(function() {
    app.listen(port);
    console.log('Your server is running on port ' + port + '.');
  });
  </code></pre>
  
  <p><strong>app/config/config.json</strong></p>
  
  <pre><code>{
    "development": {
      "username": null,
      "password": null,
      "database": "task_development",
      "host": "127.0.0.1",
      "dialect": "postgres"
    },
    "test": {
      "username": null,
      "password": null,
      "database": "task_test",
      "host": "127.0.0.1",
      "dialect": "postgres"
    },
    "production": {
      "username": null,
      "password": null,
      "database": "DATABASE_URL",
      "dialect": "postgres"
    }
  }
  </code></pre>
  
  <p><strong>app/models/index.js</strong></p>
  
  <pre><code>'use strict';
  
  var fs        = require('fs');
  var path      = require('path');
  var Sequelize = require('sequelize');
  var basename  = path.basename(module.filename);
  var env       = process.env.NODE_ENV || 'development';
  var config    = require(__dirname + '/../config/config.json')[env];
  var db        = {};
  
  if (config.use_env_variable) {
    var sequelize = new Sequelize(process.env[config.use_env_variable]);
  } else {
    var sequelize = new Sequelize(config.database, config.username, config.password, config);
  }
  
  fs
    .readdirSync(__dirname)
    .filter(function(file) {
      return (file.indexOf('.') !== 0) &amp;&amp; (file !== basename) &amp;&amp; (file.slice(-3) === '.js');
    })
    .forEach(function(file) {
      var model = sequelize['import'](path.join(__dirname, file));
      db[model.name] = model;
    });
  
  Object.keys(db).forEach(function(modelName) {
    if (db[modelName].associate) {
      db[modelName].associate(db);
    }
  });
  
  db.sequelize = sequelize;
  db.Sequelize = Sequelize;
  
  module.exports = db;
  </code></pre>