Bootcamp
Search…
4.1.10: Deploy Sequelize App to Heroku

Introduction

Deploying the base MVC (and Webpack) app is the same as deploying previous apps to Heroku with one difference- the database.
When using Sequelize we need to have different configs for connecting to the Heroku Postgres instance. Those configs are here: https://github.com/rocketacademy/webpack-mvc-base-bootcamp/blob/main/config/config.js#L9-L19​
// ...
//
production: {
use_env_variable: 'DATABASE_URL',
dialect: 'postgres',
protocol: 'postgres',
dialectOptions: {
ssl: { // https://github.com/sequelize/sequelize/issues/12083
require: true,
rejectUnauthorized: false,
},
},
},
// ...
if (env === 'production') {
// Break apart the Heroku database url and rebuild the configs we need
const { DATABASE_URL } = process.env;
const dbUrl = url.parse(DATABASE_URL);
const username = dbUrl.auth.substr(0, dbUrl.auth.indexOf(':'));
const password = dbUrl.auth.substr(
dbUrl.auth.indexOf(':') + 1,
dbUrl.auth.length
);
const dbName = dbUrl.path.slice(1);
const host = dbUrl.hostname;
const { port } = dbUrl;
config.host = host;
config.port = port;
sequelize = new Sequelize(dbName, username, password, config);
}
These are all included configs that don't need to be changed.

Initializing the DB

To get your Sequelize database running on Heroku, run the Sequelize commands within the Heroku system. (No need to create the database, that's already been done when you provision the Postgres add-on.)
heroku run npx sequelize-cli db:migrate
heroku run npx sequelize-cli db:seed:all