{ Adding a Mongo Database to Heroku. }


By the end of this chapter, you should be able to:

  • Add a Mongo Database to a Heroku Application
  • Debug a heroku application using heroku logs

Deploying and Adding mLab

To add a production database with mongo, we can use mongolab. In the root directory of your application, type the following: heroku addons:create mongolab. They may ask you for a credit card, but the layer we'll be using is entirely free. Once you have that installed you will get a config variable called MONGODB_URI which you can see by typing heroku config in the terminal.

Inside of our models/index.js, we need to now add the following line to tell our application to connect to the production database or development database, depending on the environment. Here is what that could look like:

// instead of...
mongoose.connect("mongodb://localhost/deploy_me", {
  useMongoClient: true
// we now do...
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/deploy_me", {
  useMongoClient: true

For more information, check out this guide on Heroku's site.


If you are having issues deploying your application or it is crashing, make sure you run heroku logs -t -a NAME_OF_YOUR_APPLICATION to see the server logs and debug from there. Any time you make a change remember you need to commit that change and push it to Heroku.

Sample App

You can find the previous CRUD (with associations) application set up for Heroku here.


To walkthrough the Heroku deployment process, check out this screencast:

When you're ready, move on to Deployment Exercises


Creative Commons License