Curso Django. BBDD IV. PostgreSql. Vídeo 14

Curso Django. BBDD IV. PostgreSql. Vídeo 14

Introduction to Configuring Django with Postgres

In this section, we will learn how to configure Django with the Postgres database management system. We will explore the steps required to set up and manage a Postgres database using the pgAdmin tool.

Setting Up Postgres Database

  • To work with Postgres, we need an interface that allows us to manage the database. One such tool is pgAdmin 4.
  • Download and install Postgres from the official website for your operating system.
  • After installation, open pgAdmin 4 and enter the password for accessing the database management system.
  • Once inside pgAdmin 4, navigate to "Servers" in the top left corner and expand it to find an existing default database called "postgres".
  • To create our own database, right-click on "postgres" and select "Create" > "Database". Enter a name for your new database (e.g., "prueba_articulos_clientes").
  • Click on the lightning bolt icon in the toolbar to execute the command and create the new database.
  • Refresh the left panel by right-clicking on "Databases" and selecting "Refresh". The newly created database should appear.

Connecting Django Project with Postgres

  • To connect our Django project with our Postgres database, we need to install a library called psycopg2.
  • Update your Django project's settings file (settings.py) by configuring it to use psycopg2 as the database engine.
  • Modify the DATABASES section in settings.py to include relevant details such as host, port, username, password, and name of your Postgres database.
  • Save your changes and run migrations (python manage.py migrate) to apply any necessary changes or updates to your models.

Conclusion

Configuring Django with Postgres involves setting up the pgAdmin tool to manage the database and connecting our Django project to the Postgres database using psycopg2. This allows us to work with a powerful open-source database management system for our Django applications.

Installing a Library and Changing Configuration Settings

In this section, the speaker explains how to install a library and change configuration settings in a Django project to connect with a new database.

Installing the Library

  • Use the command pip install followed by the name of the library to install it.
  • Change directory to your project directory before running the installation command.
  • The speaker already has the library installed, so it doesn't start the installation process.

Changing Configuration Settings

  • Open the settings file in your Django project.
  • Find the ENGINE, NAME, and other parameters related to connecting with Postgres in the documentation.
  • Change ENGINE from "django.db.backends.xico" to "django.db.backends.postgresql_psycopg2".
  • Update NAME with your desired database name.
  • Set USER as "postgres" or any other default username if not changed during installation.
  • Enter your password for PASSWORD.
  • Set HOST as "localhost" or use 127.0.0.1 for local development.
  • Specify the port number using PORT.

Performing Migrations and Creating Tables

This section covers performing migrations and creating tables in a Django project connected to Postgres.

Performing Migrations

  • Make sure you are in your project directory in the console.
  • Run python manage.py makemigrations to create migrations based on changes made in models.
  • If no changes have been made, it won't detect any modifications.

Applying Migrations

  • Run python manage.py migrate to apply migrations and update the database schema.

Creating Tables

  • Open your database management tool (e.g., pgAdmin).
  • Refresh or reload to see newly created tables under "Tables" within "Schemas".

Inserting a Record into the Database

This section explains how to insert a record into a table in the Django project connected to Postgres.

Importing the Model

  • In the console, import the model from your application's models.py file.
  • Use the syntax from gestion_pedidos.models import Clientes (replace gestion_pedidos with your app name).

Creating and Saving a Record

  • Create an instance of the model using variable assignment.
  • Set values for each field of the model.
  • Use double quotes for string values.
  • Save the record using .save() method on the instance.

Verifying Record Creation

  • Refresh or reload your database management tool.
  • View data by right-clicking on the table and selecting "View/Edit Data" or "Browse Rows".

Explanation of Previous Videos

The speaker explains that they will leave the video at the current point and reminds viewers that they can register for free on their virtual computer pills academy. They mention having all the free courses organized by categories, as well as offline courses for personalized follow-up with exercises, tests, doubt resolution, and certificate delivery upon completion.

Registering for Free Courses

  • Viewers can register for free on the speaker's virtual computer pills academy.
  • The academy offers a variety of free courses organized by categories.
  • Offline courses are available for personalized follow-up with exercises, tests, doubt resolution, and certificate delivery.

Timestamps were not provided in the transcript after 0:15:08 .

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Playlists: Curso Django
Video description

Vemos en este vídeo cómo configurar Django para funcionar con el gestor de BBDD PostgreSql. Para más cursos, ejercicios y manuales visita: www.pildorasinformaticas.es