Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Configure PostgreSQL database

Serhii Horodilov edited this page Feb 20, 2024 · 3 revisions

At the end of this guide you will:

  • set up PostgreSQL as a default project database

Table of Contents

Getting started

git checkout bp-models

Guide

You will need docker-engine and docker-compose installed on your computer to follow this guide.

In case, you have no these installed refer to Get project database running, and after that continue from Set up environment variables.

Docker: initialize database query

The initialization query for the docker postgres container can be found at docker/db/init.sql. And it looks like:

CREATE ROLE tasktracker WITH ENCRYPTED PASSWORD 'tasktracker' LOGIN CREATEDB;
COMMENT ON ROLE tasktracker IS 'tasktracker database maintenance role';

CREATE DATABASE tasktracker OWNER tasktracker;
COMMENT ON DATABASE tasktracker IS 'tasktracker project database';

This will create a maintenance role and a project database when the db container starts for the first time.

You can check the connection to the database using psql or pgAdmin clients. pgAdmin is also available as a docker container for this project. Refer to README file for more info.

pgAdmin: Docker container

This is an optional step, required only if you want to use pgAdmin container within this project.

pgAdmin is one of the most popular PostgreSQL clients. Starting with version 4.x, it uses a web-based UI running in your web browser. The pgAdmin container exposes its 80 port to the host machine. By default, this port is mapped to 5050. If port 5050 is already occupied by other software on your system, you may set up any available port by using the PGADMIN_PORT environment variable.

After running pgAdmin, visit http://localhost:5050 in your web browser (adjust the port number if needed).

pgadmin container credentials
Email Password
[email protected] pgadmin

When connecting to the PostgreSQL server via pgAdmin, use "postgresql-server" as the alias for the db container. This connection is already defined in the servers.json file under the docker/pgadmin directory, so there is no need to connect manually.

Note that it may take some time for the container to set up and run the internal server.

To start container use command:

docker compose up -d pgadmin

This will also start the db container.

PostgreSQL: Docker container

The db service runs the PostgreSQL server container. It exposes port 5432 to the host machine, allowing you to use it as if you had PostgreSQL running locally. The default port mapping is "5432:5432". If you already have port 5432 occupied by other software, you may set up any available port by using the POSTGRES_PORT environment variable.

postgres container credentials
username password
postgres postgres
tasktracker tasktracker

You can run this service separately from other services defined in the Compose file with:

docker compose up -d db

Set up environment variables

In your .env file complete the environment variables set up.

POSTGRES_DB=tasktracker
POSTGRES_USER=tasktracker
POSTGRES_PASSWORD=tasktracker
POSTGRES_HOST=db
POSTGRES_PORT=5432

Configure database settings

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "HOST": os.environ.get("PGHOST", "localhost"),
        "PORT": os.environ.get("PGPORT", "5432"),
        "NAME": os.environ.get("PGDATABASE"),
        "USER": os.environ.get("PGUSER"),
        "PASSWORD": os.environ.get("PGPASSWORD"),
    }
}

Apply migrations and check tables

python manage.py migrate

Changes

  1. Full change log