Use a PostgreSQL Database in Django Project
Victor Wooding
[Transcript]
In this video I am going to show you how to setup your Django project to use a PostgreSQL database.
PostgreSQL is an open source database management system. Django defaults to using a SQLite database which is server less and runs in the app whereas PostgreSQL requires a DB server to setup and run over the network.
SQLite is best used for standalone apps, small apps that don’t require expansion and can be used in development and testing.
Use PostgreSQL when data integrity and reliability is your main concern and you are working with more complex databases.
The first thing that we would need to do is install PostgreSQL if it is not already installed. Installation processes on Windows and Mac are very similar.
You can find the installers at the PostgreSQLl website. Since I am running a 64 bit system I am going to choose the 64 bit installer and I am going to select the latest version which is 13.1 at the time of recording.
Once download is complete start the installer. And we are going to leave the default settings. We don’t need stack builder so you can deselect it now or hit cancel at the end to prevent it from installing additional drivers.
Choose a default location.
We need a password for the database. You need to remember this password because we will have to enter it in the settings.py file when we configure the database.
The port is set to 5432 by default. We can leave this as it is.
Leave the locale as default.
You are next presented with a pre-installation summary. Hit next to start the installation.
Once the installation is finished you will be prompted to launch stack builder. You can deselect this option now and hit finish or hit cancel at the next screen.
I have already created my project which is called school database and I have created an app for managing students. I will add the rest framework and student to installed apps.
This is the model that I have created. It is very simple for now. I have the students first name, last name and date of birth.
Look for the databases dictionary in settings.py. Currently it is set to sql lite 3.
Replace sqlite3 with "postgresql"
Set the name to "student_db".
Set the user to "postgres".
Set the password to the password that you used during the installation of PostgreSQL.
Set the host to local host and set the port to "5432".
You can open PG admin from the start menu on windows. In Mac you will have to open it from the applications folder then right click on the icon in the task bar, copy the url and paste it in your browser.
There are two versions of postgres running on my system. 9.5 was installed with da vinci resolve. Version 13 is the version that was installed in this video.
Clicking on databases shows you the available databases. The database named ... https://www.youtube.com/watch?v=L4GSU6M6djY
25807943 Bytes