Configuring the Database
Let's see how we can configure a database in a Django project.
We'll cover the following
Now that we are familiar with the structure of a Django project, let’s learn how to configure the project to connect to a database.
Django, by default, uses sqlite3 as a database, which is an in-process library that implements a fast self-contained, zero-configuration, serverless, transactional SQL database engine. It’s very compact and easy to use and set up. It’s ideal for testing or if we're looking to save data quickly. However, it comes with some disadvantages.
First of all, there are no multi-user capabilities, which means that it comes with a lack of granular access control and some security capabilities. This is due to the fact that SQLite reads and writes directly to an ordinary disk file.
For example, in our project, after running the migrations, we’ll notice the creation of a new file, db.sqlite3
. Well, this is actually our database!
We will be replacing it with a more powerful SMDB called Postgres.
Postgres configuration
PostgreSQL is one of the world’s most advanced enterprise-class open source database management systems, developed and maintained by the PostgreSQL global development group. It’s a powerful and highly extensible object-relational SQL database system that comes with interesting features, such as the following:
User-defined types
Table inheritance
Asynchronous replication
Multi-user capabilities
These are the features you will be looking for in a database, mostly when working in a development or production environment.
Note: Please look at the Appendix to learn to set up Postgres on your local machine.
Creating database
Let’s quickly create the database we’ll use for this project.
For that, we need to connect as a Postgres user in the terminal and then access the psql
terminal. In that terminal, we can enter SQL commands.
We can log in as follows:
Get hands-on with 1200+ tech skills courses.