Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

postgresql
django
database

How to connect PostgreSQL with Django

Abdul Monim Tariq Lodhi

As we already know, Django generates a database by default in the form of SQLite.

Django app with SQLite database

A superuser has been created using an SQLite database, given below.

  • We start the terminal by clicking the “Run” button.

  • Now, we can find the application by clicking on the given link below the “Run” button.

  • To see the login page for the Django admin site, we need to append /admin/ at the end of our base URL. For example, if the application is running at http://127.0.0.1:8000, we can access the admin panel at http://127.0.0.1:8000/admin/.

Note: We will use “educative” as the user name and “edu123” as the password.

"""
ASGI config for my_project project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')

application = get_asgi_application()

The Django application with PostgreSQL database

We can connect PostgreSQL with Django by following these simple steps.

Note: PostgreSQL has already been installed on our platform.

  • Start the terminal by clicking the “Run” button in the given example below.

  • Start the service by typing the given command on the given terminal.

service postgresql start
  • We can alter the password of the superuser of PostgreSQL by typing the given command.
su - postgres -c "psql -d postgres -U postgres -c \"alter user postgres with password 'password123';\""
  • We can create a database using the given command.
su - postgres -c "psql -d postgres -U postgres -c \"CREATE DATABASE example;\""
  • The database section has been replaced in settings.py.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'example',
        'USER': 'postgres',
        'PASSWORD': 'password123',
        'HOST': 'localhost',
    }
}
  • Now, we type the following commands step by step:

    • cd /usercode
    • python3 manage.py makemigrations
    • python3 manage.py migrate
  • Next, we create a superuser by writing the following command.

python3 manage.py createsuperuser

Note: A superuser will be created if we have entered all fields correctly.

  • To run the server, we type the following command.
python3 manage.py runserver 0.0.0.0:8000
  • Now we can find the application by clicking on the link given below the “Run” button.

  • We append /admin/ at the end of the base URL to access the admin panel.

"""
ASGI config for my_project project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')

application = get_asgi_application()

RELATED TAGS

postgresql
django
database

CONTRIBUTOR

Abdul Monim Tariq Lodhi
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring