Database configuration for Laravel #1. Heroku

James Falola
Hacktive Devs
Published in
2 min readMay 16, 2019

If you got your app deployed to Heroku safely, and you need to add a database to your application, here’s how you can go about that

My last published article was about Laravel in production where we deployed to Heroku.

Now we’ll proceed to add the database configuration for our Laravel app on Heroku.

Here’s the link to the previous article. Read up the article if there is any issue deploying your app to Heroku.

  • Add Database addon to our app

You can either do that from your Heroku dashboard or from your terminal.
We’ll create our addon from our terminal.

Heroku doesn’t allow you to add MySQL as an addon until you add a payment method to your account. If like me, you don’t have functioning ATM card, do not panic, you can use PostgreSQL add-on and this wouldn’t break your code.

heroku addons:create heroku-postgresql:hobby-dev

This would be added to your application Config Vars. Config vars are the equivalent of your environment variables on Heroku.

  • Change app DB connection.

Here we’ll make our app work with PostgreSQL, from Laravel’s database config file (Config/database.php), you can see we have more than one database connection present, each one with a different driver. Now we’ll configure our app to use the pgsql connection from Heroku.

heroku config:set DB_CONNECTION=pgsql

Are we there yet? I’m sorry, the answer is no. We still need to make sure our app understands the config variable DATABASE_URL. On the brighter side, you won’t have to worry about this again from the next Laravel update.

  • Make our app work with DATABASE_URL

Change your database config file to look like this :

And there you have it. Commit your changes and deploy to Heroku again.

  • Run migration on Heroku
heroku run php artisan migrate

You made it to the end! Got any questions? Leave a comment below.
We’ll go through Database Setup in CPanel in a follow-up article.
Thanks for your time.

--

--