PostgreSQL with Django Django.How

Author avatar wrote on 26/05/2022

PostgreSQL is a more advanced DB server than MySQL, it’s free and open-source, ISO compliant, and has more advanced features like;

    • Rich datatypes (arrays, maps, json)

  • Rich Geospatial Support (postgis)

  • Great JSON Support

  • Custom Data Types – create your own type

  • Rich Fulltext Support

  • Highly Conformant to SQL Standards

  • Nonblocking index creation

  • Partial Indexes

  • Common Table Expressions – a feature that lets you simplify your queries

  • Analytics Functions

  • Replication Features – can copy itself to a second DB automatically

  • Window Functions – They are a set of aggregate functions that allow you to calculate the result of an aggregate over several rows, but still display the details of individual rows. For example, you could display all student records, their final test score, as well as the average test score for their age or group.

  • Custom Languages and Functions Inside the Database – create your own

  • Foreign Data Wrapper – It lets you create a foreign table which refers to an external PostgreSQL database.

  • Extensions – There are any extensions available for PostgreSQL, which allow you to easily add on to the existing functionality of the database.

Resources:

Install PostgreSQL

Resource https://www.udemy.com/course/python-django-dev-to-deployment/learn/lecture/12056336

  1. Head to https://www.postgresql.org/download/

  2. On windows use this tool https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

  3. Make sure to install pgAdmin, a graphical interface to manage your DBs

  4. Open pgAdmin and create a DB

Create Server and Database

  • Open the PGAdmin app

  • If there is no server, create one

  • under connection add username and

  • hostname is localhost

  • Create a database

  • right-click to open properties

  • under security → privileges add your user privileges all

Configure PostgreSQL for Django Project

1- instal PostgreSQL on project

pip install psycopg2

pip install psycopg2-binary

2. Go settings.py and change the database setting to the info of the new server


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'btredb',
        'USER': 'postgres',
        'PASSWORD': 'admin',
        'HOST': 'localhost',
    }
}
  

3. Migrate Tables

python manage.py migrate

You can now see on pgAdmin that the default Django tables (10 tables) are created

CRUDgen Django CRUD generator