DEV Community

Pedro Campos
Pedro Campos

Posted on • Edited on

Django project - Part 2 Postgres

This is the part 2 of a serie of posts on how to structure a Dango project. This is the link to part 1.

The source code from this part

Introduction

Ok, we have our Django project running on docker, let's now configure the Postgres. Dispite the fact that we can use sqlite in production, we are going to use the Postgres.

Update compose.yaml

We are going to add a new service for postgres and configure as a dependence to our web service.

services:
# The service to be created on docker
  web:
    ...
    # Only start this image after postgres
    depends_on:
      - postgres

  postgres:
    # The postgres image from docker hub
    image: postgres:16.2
    container_name: palindrome_postgres
    # The volume that will be used by this image
    volumes:
      - palindrome_postgres_data:/var/lib/postgresql/data
    # Open the port in localhost:5432 for external access
    ports:
      - "5432:5432"
    # db NAME, USER and HOST are by default 'postgres', just configure the password here
    environment:
      POSTGRES_PASSWORD: postgres

volumes:
    # Volume used by the postgres container
    palindrome_postgres_data: { }
Enter fullscreen mode Exit fullscreen mode

Update .env file

...
# DB_PASSWORD is configured on compose.yaml, the others is the image default 
DB_NAME=postgres
DB_USER=postgres
DB_PASSWORD=postgres
DB_HOST=postgres
DB_PORT=5432

Enter fullscreen mode Exit fullscreen mode

Update the .env.template for first setup refenrece

Add the template on git

... 
DB_NAME=
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=
Enter fullscreen mode Exit fullscreen mode

Add psycopg on Poetry

Add psycopg, a python adapter to postgres, so we can talk to the database.

$ poetry add psycopg[binary]
Enter fullscreen mode Exit fullscreen mode

Update settings.py

Configure settings.py for using postgres and remove the sqlite.

DATABASES = {
    # Remove the sqlite configuration
    # 'default': {
    #     'ENGINE': 'django.db.backends.sqlite3',
    #     'NAME': BASE_DIR / 'db.sqlite3',
    # }

    # Add the postgres configuration
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': env('DB_NAME'),
        'USER': env('DB_USER'),
        'PASSWORD': env('DB_PASSWORD'),
        'HOST': env('DB_HOST'),
        'PORT': env('DB_PORT'),
    }

}


Enter fullscreen mode Exit fullscreen mode

Update README.md

Yes, this is part of the job. Just add the psycopg

# Palindrome project

Project used to explain my view on a django project architecture, explained on my [series of posts](https://dev.to/pcampos119104/django-project-setup-part-1-2e7a)

## Tools, libs, etc. Some time related files.

Versions on Poetry.

- [Python](https://www.python.org/) Programming languange
- [django-environ](https://django-environ.readthedocs.io) Manage .envs in Django
- [Poetry](https://python-poetry.org/) Python packaging and dependency management
    - poetry.lock
    - pyproject.toml
- [Django](https://www.djangoproject.com/) Web framework written in Python
- [Docker](https://www.docker.com/) Manage containers for dev environment
    - compose.yaml
    - compose/dev/Dockerfile
    - compose/dev/start
    - .env
- [Just](https://just.systems/) encapsulate commands for easier use
    - justfile
- [psycopg](https://www.psycopg.org/) Python adapter for Postgres # <-- new line

## Dev environment setup

1. Install Just, Docker and Poetry(opcional).
2. Copie .env.example to .env, no need for edtion. 
3. `$ just build`

## Run the server for development

1. Certified that docker is up and running
2. `$ just runserver`

You can access on http://0.0.0.0:8000/
Enter fullscreen mode Exit fullscreen mode

Rebuild an run the project

$ just build
$ just runserver
Enter fullscreen mode Exit fullscreen mode

You should be able to create a superuser normally now. Just run just mng createsuperuser

Top comments (0)