🐘 Set up A PostgreSql Database in Django
Tags: postgres, django, python, database
Published: 2021-03-05 01:44:36

# Quick advice:

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

{% post  https://dev.to/developerroad/tutorial-deploying-a-django-app-on-heroku-4k6o %}



##  Creating an app on heroku

Before you create a free app on heroku you must create an account, [here](https://signup.heroku.com/).

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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8l9cq221iqs4ddfjwj1.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjo06vootz8p4ejkdxj4.png)

Now that you've created a new app, click in "Overview", and select configure addons.![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwwwuygff011pa2z91iu.png)

Now search for `heroku postgres` and select it.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkalds93a3ygw3b904az.png)
  
# 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
```python
pip install gunicorn django-heroku python-decouple
```

I'll be only explaining the database set up, for other instructions, just [read this tutorial](https://dev.to/developerroad/tutorial-deploying-a-django-app-on-heroku-4k6o)

## 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:
```python
# 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')
    )
}


```

Create a Procfile
# Run this in the root directory of the app

```
touch Procfile
```

Open the file and copy the following line:
```
web: gunicorn (Your app).wsgi
```
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
```

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
```

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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qu7kbqprvmbcgejb2zkm.png)

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

Now connect the repo you want to deploy

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7snt1rsf5l9s95fseag.png)
 
And finally in Manual deploy, click  on `Deploy branch`.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0dxgqy2kd7jy26jgc2a.png)

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

Well just read the [Heroku docs](https://devcenter.heroku.com/articles/deploying-python) , or comment below your problems.

Thanks for your time hopefully this tutorial will be useful for you 🤗. 

>Follow me in [My blog](https://developerroad.herokuapp.com/blog/), 
 to get more awesome tutorials like this one.
 Please consider supporting me on [Ko-fi](https://ko-fi.com/daniel1404) you help me a lot to 
 continue building this tutorials!.
[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Y8Y03P8P1)