Categories
Python Answers

How to set up a PostgreSQL database in Python Django?

Spread the love

Sometimes, we want to set up a PostgreSQL database in Python Django.

In this article, we’ll look at how to set up a PostgreSQL database in Python Django.

How to set up a PostgreSQL database in Python Django?

To set up a PostgreSQL database in Python Django, we add the psycopg2 package and add some settings into settings.py.

We run

pip install psycopg2

to install psycopg2.

Then in settings.py, we write

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_name',                      
        'USER': 'db_user',
        'PASSWORD': 'db_user_password',
        'HOST': '',
        'PORT': 'db_port_number',
    }
}

to set the DATABASE variable to a dict with the settings.

USER and PASSWORD ahs the user and password.

HOST and PORT has the URL and port of the database server.

Conclusion

To set up a PostgreSQL database in Python Django, we add the psycopg2 package and add some settings into settings.py.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *