DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Easiest Way To Connect Django To A Postgres Database
Dennis Ivy
Dennis Ivy

Posted on

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.

Top comments (5)

Collapse
 
geetechlab profile image
Gerard Nwazk

Nice it worked for me 100% but on local development. i was having issues using it in production using Docker on heroku cloud. do you have similar challenges or have you tried using it for production?...

Collapse
 
edivaldolluisb profile image
Edivaldo G. Castro Luís Bonfim

may I call you to give me a help? I facing a connection error

Collapse
 
edivaldolluisb profile image
Edivaldo G. Castro Luís Bonfim

I'm getting this error running migrate why?

Image description

Collapse
 
chidera777 profile image
Ferdinand

I am getting this error when I fill all the values
Image description

Collapse
 
nasimhc profile image
Nasim

It looks like there is problem with the connection.

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.