DEV Community

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 (9)

Collapse
 
2062glossyledge profile image
Ayden Smith

Hey, I tried to deploy my web app to vercel but I'm getting this error: Error: pg_config executable not found. It then directed me to install the package psycopg2-binary, which I did but I'm still getting the same error.

Collapse
 
geetechlab profile image
Gerard Nwazuruoke

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
 
jane49cloud profile image
Jane49-cloud

This is awosome....My first time to use postgresql and you made it fun and easy...Thank you

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

I'm getting this error running migrate why?

Image description

Collapse
 
samnw_ profile image
Samuel N. Wanyoike • Edited

To anyone having this error, I had it and solved it by installing a Python package called "dj_database_url" using "pip install" and importing it inside the settings.py file. It works to configure database urls in single-string variables. Here is how to use it after collecting the single string database URL from the railway Postgres db you created:

DATABASES = {
'default': dj_database_url.config(default=os.environ.get('DATABASE_URL'), conn_max_age=1000)
}

make sure to define the environment variable database URL, the string obtained from railway under the variable name "DATABASE_URL".

Goodluck!

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.

Collapse
 
pgsaisuhas profile image
Pgsaisuhas

can you share this with others? like the database details, so that our team can work on the same project?