Easiest Way To Connect Django To A Postgres Database
Tags: djano, python, postgres
Published: 2022-07-20 21:52:04

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

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

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


## 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](https://railway.app?referralCode=W9Uj72)

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

![New project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n24zs972zx6omj11m7kn.PNG)

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

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

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

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

## 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.