DEV Community

Cover image for Database: Types, Configuration and Migration
Akolade
Akolade

Posted on

Database: Types, Configuration and Migration

Django comes with its own default database SQLite, which is a lightweight and simple relational database management system.

While SQLite is a good choice for development and testing, it may not be suitable for production environments due to its limitations such as poor performance, limited concurrency, lack of scalability, limited features, lack of built-in tools for maintenance and lack of built-in security features.

If you're planning to deploy a Django application in production, you should consider using a more robust and scalable database management system like PostgreSQL or MySQL.

How to Select A Database

The decision of whether to use a SQL or NoSQL database with Django depends on the specific requirements of your project.

SQL databases, such as PostgreSQL or MySQL, are well-suited for projects requiring high data integrity and consistency. They provide a structured way of storing and querying data and are well-integrated with Django's ORM (Object-Relational Mapping) system, making it easy to create, query, and modify data using Python classes and objects. SQL databases are also good for projects that have a lot of relationships between data and need to be able to perform complex JOIN operations.

NoSQL databases, such as MongoDB, are well-suited for projects that handle a lot of unstructured data and require horizontal scalability. They don't have the same level of data integrity and consistency as SQL databases, but they can handle a large amount of data and are designed to scale horizontally. NoSQL databases are also a good fit for projects that need to store and retrieve large amounts of unstructured data, like JSON documents.

It's important to note that Django doesn't officially support NoSQL Databases but they can be integrated using a third-party library. Read more here

Let's configure a Database in our Django settings

I will be using Postgres for this. you can as well check out how to configure a database using MySQL

1. Install and configure Postgres.

Download Postgres here for your operating system.

Install it. Then start your Postgres server.

2. Install dependencies or database adapter

In your environment, install psycopg2: pip install psycopg2

we need to now tell Django that we have changed our database.

You know Django comes with a default configuration for the SQLite database, we need to override that configuration

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
Enter fullscreen mode Exit fullscreen mode

For Postgres configuration, the following variables are required.

  • DB name

  • DB user

  • Password of the User

  • Host and Port

  • Engine

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'blog',
        'USER': 'postgres',
        'PASSWORD': '123456789',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
Enter fullscreen mode Exit fullscreen mode

Note: I won't cover how to create a database schema with Postgres. You can look that up in a video tutorial.

Migration

After installation and configuration, what is next? yes, you got it. we need to make migrations. this is because we now have a fresh and clean database instance.

makemigrations: we need to run this command python manage.py makemigrations . This command is used to create new migrations files based on the changes detected in the models. These migration files can then be used to create or modify the database schema to match the current state of the model.

then we will run the migrate command: python manage.py migrate when you run this command, Django will look at the migration field that was created by the makemigrations command and use them to create and modify the database schema.

Conclusion

Now that we understand how to set up our database, it is also advisable to make more research to get a better view and understanding of databases.

You can follow me on Twitter, I write about Django weekly.. cheers and happy coding

Top comments (0)