DEV Community

Cover image for 🐘 Set up A PostgreSql Database in Django
Daniel Diaz for Developer Road

Posted on • Updated on • Originally published at developerroad.herokuapp.com

🐘 Set up A PostgreSql Database in Django

Quick advice:

If you want a post that covers the deploy of a Django app, entirely go to this one

Creating an app on heroku

Before you create a free app on heroku you must create an account, here.

Once you have created your account, you will have a dashboard like this one.

image

To create an app click in New, and you will see a dropdown, with the option of Create new app.

Choose that, select a name and click on create app.
image

Now that you've created a new app, click in "Overview", and select configure addons.image

Now search for heroku postgres and select it.
image

Modifying your settings file

Now you are almost ready to deploy to heroku, but first let's modify your settings, to be ready to be deployed.

Install django_heroku and python decouple

pip install gunicorn django-heroku python-decouple
Enter fullscreen mode Exit fullscreen mode

I'll be only explaining the database set up, for other instructions, just read this tutorial

Using dj_database_url

dj_database_url is a package that comes bundled, with django_heroku.

It's function dj_database_url.config(), receives as a parameter the url config of a database, and transform that in the needed keys to get access to it.

Modifying:

# Import this packages previously dowloaded

import django_heroku
import dj_database_url
from decouple import config

# Go down until you find database stuff

# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases


# Commment out the sqlite database setup

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': BASE_DIR / 'db.sqlite3',
#     }
# }

# These lines make all the magic
DATABASES = {
    'default': dj_database_url.config(
        default=config('DATABASE_URL')
    )
}


Enter fullscreen mode Exit fullscreen mode

Create a Procfile

Run this in the root directory of the app

touch Procfile
Enter fullscreen mode Exit fullscreen mode

Open the file and copy the following line:

web: gunicorn (Your app).wsgi
Enter fullscreen mode Exit fullscreen mode

This tells Heroku, that it must run a web process, with gunicorn as HTTP server and with a wsgi file located in (Your app). Remember to change (Your app) with the name of your project.

Freezing requirements.txt

To freeze a file with the requirements of your application, just run

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Heroku will search for that file, so don't forget to run that command.

Managing the deploy with Github

Deploys with Github are way easier than with the Heroku CLI tool, so don't forget to create a repository on Github with the app you are trying to deploy.

Push the changes you've made with

git add -A
git commit -m "Database Settings ready"
git push origin
Enter fullscreen mode Exit fullscreen mode

Now in the heroku website, go to the Deploy tab and click in Connect with github.

image

You will be redirected, to Github in order to authorize access to your repos.

Now connect the repo you want to deploy

image

And finally in Manual deploy, click on Deploy branch.
image

Wait for it, if you get errors ....

Well just read the Heroku docs , or comment below your problems.

Thanks for your time hopefully this tutorial will be useful for you πŸ€—.

Follow me in My blog,
to get more awesome tutorials like this one.
Please consider supporting me on Ko-fi you help me a lot to
continue building this tutorials!.
ko-fi

Top comments (3)

Collapse
 
hnsmchl profile image
hnsmchl

Hello there, I receive this error message when trying to migrate the settings.py

decouple.UndefinedValueError: DATABASE_URL not found. Declare it as envvar or define a default value.

any solutions?

Collapse
 
danidiaztech profile image
Daniel Diaz

Any question, or trouble? Let me know!

Collapse
 
hnsmchl profile image
hnsmchl

hello there, I've posted a comment regarding a problem I have.