Deploy django application with postgresql database into heroku

Eby Sofyan
4 min readDec 6, 2019
Source: https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSsy9hqBgiRPmnzKuYbU_dceYGkj4Baqtl90ewMT3gEJLHRMRHh

Deployment is one of the great stuff in software development cycle. Some great application will useless when there is no one can access it. The good code will not be applicable, when it is just a partially of another code. Currently, there are too much technology offers us to make our deployment more easier. This is automation era, config once deploy whene ever you want.

Why Heroku?

We are all know about the platform as a service, that platform provides an ecosystem to make us easier to manage out application.

But for your information, the reason above is not my reason why i choose heroku as a platform. The main reason is Heroku is one of the platform as service that provides a free plan for developer 😆

Clear?

Okayyy stop too much talk about my useless theory, let’s do it with some sample code 😃

Talk is cheap, show me the code ~Linus Torvald

In this section, we will deploy our django application into heroku and connect it with postgresql database.

What we need before we do this great stuff?

  1. You have created django application that installed a several libraries using pip,pipenv,conda such as gunicorn , python-dotenv and dj-database-url
  2. herouku-cli already installed on your machine. If not, follow this link
  3. You already have a cup of coffee

Let’s follow step by step

Step 1. Makesure you have application already created on your heroku account. If not, you can create it first using dashboard gui or using heroku-cli

Step 2. Add heroku-pstgresql addon on your dashboard exactly on Resources tab

Step 3. Heroku will automatically configure your addon and make it configurable by clicking the addon. FYI: You can find url or credential of your database on detail of addon exactly in Settings -> view credentials . For detail, you can see following image.

Step 4. Configure your django application ready to use .env file to manage your environment variable, or you can see the following snippet.

import os
import dotenv
dotenv_file = os.path.join(BASE_DIR, ".env")if os.path.isfile(dotenv_file): dotenv.load_dotenv(dotenv_file)

Put it all on your settings.py file

Step 5. Already create .env file? If not, why just read this tutorial, create it!!!

Put this line of code

DATABASE_URL=sqlite:///db.sqlite3

Above line of code just sample using sqlite3 if you want to use mysql or another database, you can change it with valid database url.

Step 6. Back to your settings.py change following line of code

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'sqlite3.db'),
}
}

to

import dj_database_urlDATABASES = {}DATABASES['default'] = dj_database_url.config(conn_max_age=600)

Above line of code will make your DATABASE_URL adjustable to detect your local env settings and container env settings.

Important! : don’t forget to add .env file to .gitignore, .dockerignore, or .slugignore

Step 7. Configure your heroku env variable by type this command heroku config -a your_application_name You will see

Heroku already config postgresql connection for you 👌

Step 8. Ahhhh forgot . . . The first thing should we do before doing something else with heroku-cli We need to login first to make heroku read our account by run this following command

heroku login

Then

heroku containe:login

Step 9. Complete your heroku deployment by following this command

heroku container:push web -a your_application_name

Then

heroku container:release web -a your_application_name

And tada . . . . . your app already deployed into heroku, and you can access your heroku app by accessing your application url.

But, this step is not enough, because your django application couldn’t run properly because you forgot the magic django command to execute.

Step 10. This is last step but not least, you forgot to run makemigrations and migrate your model migrations. Don’t panic i’m here . . .

Access your container bash cli by run this following command

heroku run bash

You will get full access to your container environment, and do migrations and migrate

python manage.py makemigrations
python manage.py migrate
pyhton createsuperuser --username admin --email email@admin.com

This is pretty simple, right? Heroku do it all for us. You can repeat this step for your new application.

If you want to see full sample of code, visit my Github link below

Now, go ahead and clap for this article if it usefull. Thank you.

--

--

Eby Sofyan

Software developer with almost 3 years experience especially in Backend and Android Developmnet. Using Django and Kotlin as weapon.