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 insta…


This content originally appeared on DEV Community and was authored by Dennis Ivy

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.

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

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>',
    }
}

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.


This content originally appeared on DEV Community and was authored by Dennis Ivy


Print Share Comment Cite Upload Translate Updates
APA

Dennis Ivy | Sciencx (2022-07-20T21:52:04+00:00) Easiest Way To Connect Django To A Postgres Database. Retrieved from https://www.scien.cx/2022/07/20/easiest-way-to-connect-django-to-a-postgres-database-2/

MLA
" » Easiest Way To Connect Django To A Postgres Database." Dennis Ivy | Sciencx - Wednesday July 20, 2022, https://www.scien.cx/2022/07/20/easiest-way-to-connect-django-to-a-postgres-database-2/
HARVARD
Dennis Ivy | Sciencx Wednesday July 20, 2022 » Easiest Way To Connect Django To A Postgres Database., viewed ,<https://www.scien.cx/2022/07/20/easiest-way-to-connect-django-to-a-postgres-database-2/>
VANCOUVER
Dennis Ivy | Sciencx - » Easiest Way To Connect Django To A Postgres Database. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/20/easiest-way-to-connect-django-to-a-postgres-database-2/
CHICAGO
" » Easiest Way To Connect Django To A Postgres Database." Dennis Ivy | Sciencx - Accessed . https://www.scien.cx/2022/07/20/easiest-way-to-connect-django-to-a-postgres-database-2/
IEEE
" » Easiest Way To Connect Django To A Postgres Database." Dennis Ivy | Sciencx [Online]. Available: https://www.scien.cx/2022/07/20/easiest-way-to-connect-django-to-a-postgres-database-2/. [Accessed: ]
rf:citation
» Easiest Way To Connect Django To A Postgres Database | Dennis Ivy | Sciencx | https://www.scien.cx/2022/07/20/easiest-way-to-connect-django-to-a-postgres-database-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.