Creating a PostgreSQL DB on AWS and Connecting it to Heroku Django app
By: Mrudula M
Tags: Heroku, Aws Rds, Django, Database Connection, Postgresql

Creating a PostgreSQL DB on AWS and Connecting it to Heroku Django app Mrudula M Follow Jul 7 · 5 min read

Photo by Cookie the Pom on Unsplash

When running your Django app on Heroku, you might be interested in using AWS RDS as your database server with a PostgreSQL engine because setting it up is cheaper and customizable to exactly what your application’s needs are. You can have granular control over access, security, monitoring, alerts, geographic location, and maintenance plans too.

In this tutorial, we would be creating an AWS RDS instance, connecting it to PostgreSQL database and then linking it to our Django app hosted on Heroku. In simple words, making database go live on AWS !

Prerequisites : Django App hosted on Heroku.

1.Create a Database on AWS

First, lets sign up and create an AWS account(enter your credit card details, but wont be charged as we are gonna use a free tier right now). You can do this here aws.amazon.com. Once all set, Sign in to the console by entering the user details. In the AWS Management Console, enter “RDS” in the find services search area and click on it.

Click on the Create Database button and follow the below steps leaving remaining defaults unchanged

In the Engine Options, select “PostgreSQL”

Create database>Engine options> PostgreSQL

In Templates section, select “Free tier”

Moving on to Credential settings, create a Master username, Master password and Confirm the password (remember to make a note of these as we would be using them later)

Create database>Settings>Credentials Settings

Under Connectivity, click on Additional connectivity configuration and set the Publicly accessible to “Yes”

Create database> Connectivity> Additional connectivity> Publicly accessible

In Additional configuration, enter an Initial database name. (Make a note of this one too).

Create database> Additional configuration> Database options

Now, scroll to the bottom and click on Create database button. It will take few minutes and once done will display a success message.

Once ready, click on the database to see its details. Make a note of the “Endpoint” url and Port number(usually it is 5432) under the Connectivity & security.

Below, in the Security group rules section, click on the default group for Inbound type.

RDS> Databases> database-1> Security group rules

In the new window, click on Actions > Edit inbound rules. Here, change the Type to “PostgresSQL” and the Source to “Anywhere” from the drop down lists and click on Save rules button.

This concludes the AWS settings.

2. Connect the above to PostgreSQL in our pgAdmin.

Prerequisites here would be to download and install PostgreSQL and pgAdmin in your system, and when you launch pgAdmin in your browser , you would be prompted to enter your PostgreSQL password(created during installation) .

In pgAdmin, right click on the Servers> Create> Server Group. Enter a name for the server group. Next, right click on the created Server group> Create> Server. Enter a server name.

Under the Connection tab, in Host name enter the AWS endpoint url , in the Port, Username and Password section, enter the AWS Port(5432) , Master username and Master password we made a note of when creating the database in AWS. Click on Save button.

pgAdmin> Sever group> Server> Connection

Now the connection is made and the database is created, which can be accessed by clicking on database name> Schemas > Tables. (The tables would be empty as we have to migrate them yet, will do this in the end)

3. Configure the database in Django project settings

Coming to our Django project, pip install psycopg2 (This is a database adapter that allows PostgreSQL to talk to any python program)

pip install psycopg2

And pip install dj-database-url(helps to configure database url environmental variable in Heroku).

pip install dj-database-url

Don’t forget to update the two packages in requirements.txt file.

Next, in Django settings.py remove the sqlite database and add the below-

import os DATABASES = {

'default': {

'ENGINE': 'django.db.backends.postgresql',

'NAME': os.environ.get('DATABASE_NAME'),

'USER': os.environ.get('DATABASE_USER'),

'PASSWORD': os.environ.get('DATABASE_PASSWORD'),

'HOST': os.environ.get('DATABASE_HOST'),

'PORT': '5432'

}

}

import dj_database_url

db_from_env = dj_database_url.config(conn_max_age=600)

DATABASES['default'].update(db_from_env)

We shall set the above environmental variables in Heroku website.

Next, we would be downloading and installing AWS RDS SSL Root Certificate.

This file helps in encrypting the connection to our Database. You can download it here. Choose the link that describes your database. Clicking on the link would download the file to your system.

Rename the file to “root certificate.pem” and save it directly into the root directory of Django project

Now add the above changes to git( git add -A) , commit( git commit -m “Added database connections”) and push to Heroku master branch (git push heroku master)

4. Setting up database in Heroku.

Now, login to Heroku account and click on your app . Click on Resources and in the Add-ons section, Delete the default database created by Heroku, as we would be pointing it to our AWS database.

Next, we need to manually set configuration variables. Go to the Settings tab and click on Reveal Config Vars. Here, we will add all the environmental variables and the database url to connect our AWS database to Heroku app. Add the below KEY: VALUE pairs and enter the data accordingly-

DATABASE_HOST : <enter your AWS endpoint url>

DATABASE_NAME: <enter your AWS Initial database name>

DATABASE_USER: <enter your AWS Master username>

DATABASE_PASSWORD: <enter your AWS Master password>

DATABASE_URL: < construct as below>

We have to construct the DATABASE_URL as postgres://username:password@endpoint url:port/dbname

We need to set all the AWS details in Heroku Config Vars

Once all the environmental variables are saved, we can now migrate our tables by running the below command in our command prompt

heroku run python manage.py migrate

Create a super user by running the command

heroku run python manage.py createsuperuser

Now you should be all set! Our database is live and connected to Heroku app, we can access our tables in pgAdmin.