This guide explains how to create a PostgreSQL database, configure it for use in Django, and migrate models to the database.
- PostgreSQL must be installed and running on your system.
- Access to the PostgreSQL command-line tool (
psql
). - Create a virtual environment using
python3 -m venv myenv>
- Open a terminal and log in to
psql
with a superuser account:psql -U postgres CREATE DATABASE "USER";
Open your Django project's settings.py file.
Add this configuration to a .env file like this:
DATABASE_HOST=localhost DATABASE_PORT=5432 DATABASE_PASSWORD=root DATABASE_USERNAME=postgres DATABASE_NAME=USER DATABASE_ENGINE=django.db.backends.postgresql
For docker set at DATABASE_HOST=host.docker.internal
1. Generate Migration Files and Apply the Migrations: FOLLOW THE ORDER, Run the following command to generate migration files for the models in your application. After each generation execute the migrations to create the necessary tables in the database:
python manage.py migrate
Django will now map the models in your project to the PostgreSQL database and create the corresponding tables.
In order to set up de development environment using docker compose run the following commands
1. Create the services
docker compose -f docker-compose-dev.yml up --build -d
It will create 2 services. A postgres database and the API. After
2. Run the migrations inside the api container
docker exec jaws-api python3 manage.py migrate
With that command python will create all the database schema up-to-date