Easiest Way To Connect Django To A Postgres Database

dennisivy11

Dennis Ivy

Posted on July 20, 2022

Easiest Way To Connect Django To A Postgres Database

In this demo I'm gonna show you how to provision a production level postgres database in minutes and connect it to your Django app.

AWS can be complex

In most of my projects and tutorials I use postgres with an Amazon Web Services RDS instance. That works in many cases but I hate the level of complexity AWS has to it. Sometimes it's nice to just get up and running FAST.

So, yesterday while scrolling on twitter I saw a post about railway and decided to give it a shot. I was amazed by the speed and simplicity of the connection!

So without further introduction, lets create a blank Django project, create a database and connect it.

Basic Django App

Note: Ensure you have python installed



pip install django
pip install psycopg2
django-admin startproject railway_django
cd railway_django


Enter fullscreen mode Exit fullscreen mode

Note: Psycopg2 is a popular postgres database adapter for python so we'll need it when connecting django to postgres

In your projects settings.py file change the database ENGINE string from sqlite3 to postgresql and add NAME, USER, PASSWORD, HOST and POST as keys to the default dictionary.



DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}

Enter fullscreen mode Exit fullscreen mode




Postgres Database with Railway

Railway has a great free tier so we wont need a card on file to start.

Step 1: Create an account on railway.app

Step 2: In your dashboard (railway.app/dashboard) click "+ New Project" and select "Provision PostgresSQL". It should take a few seconds for your database to be ready.

New project

Step 3: Once your database is ready, select the new database and go to the "Connect" tab. Here you will see your "Postgres Connection URL".

Connection URL

This connection URL may seem like a bunch of random character's so lets extract all the values from this url by selecting the "Variables" tab if you want a more user friendly version of this connection.

Here we can hover over each section and see the actual values in each part of the connection string.

Image description

Go ahead and stick to the default values provided and update our connection in our django settings.py file.



DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '<PGDATABASE>',
'USER': '<PGUSER>',
'PASSWORD': '<PGPASSWORD>',
'HOST': '<PGHOST>',
'PORT': '<PGPORT>',
}
}

Enter fullscreen mode Exit fullscreen mode




Migrate and View

That's it for the connection! Go ahead and run python manage.py migrate and view your data in the data tab in your postgres database on railway.

Go ahead and create a new user and refresh the database to see actual data appear.

💖 💪 🙅 🚩
dennisivy11
Dennis Ivy

Posted on July 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related