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

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...
// we now do...
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/deploy_me')


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.

When you're ready, move on to Deployment Exercises