Use a PostgreSQL Database in Django Project



Use a PostgreSQL Database in Django Project

Use a PostgreSQL Database in Django Project

[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 postgres was created by default.

Return to the editor and lets run make migrations.

This gives an error because we are missing a module. I am doing it this way because I want to show you some of the errors that may occur when trying to migrate to a PostgreSQL database.

Install psycopg2 using pip and if you are on a mac you will have to add hyphen binary to that command.

Run makemigrations again and this time you notice that we get an error stating that the database does not exist.

Return to pgadmin and right click on databases and select create. Name the database student_database and hit save.

If we expand the database and click on the tables we can see that the tables are empty.

Return to the editor and run makemigrations again and this time the operation is successful.

Run migrate to commit the changes to the database.

Now let’s try to store data in the database. To do this we will need to create a superuser.

Log in with the credentials that you just created.

In order to see the model we need to register it in admin.py.

Now we can create student objects.

If you refresh the database you should now be able to see the database tables. This indicates that our server is up and running.