Database Setup

This page is dedicated to db setup via docker

Prerequisites

  1. Install Docker Desktop: Download and install Docker Desktop.

  2. Verify Docker Installation: Ensure Docker is running and verify with docker --version in your terminal.


Docker Configuration

  1. Use a Docker Compose File: Below is a docker-compose.yml for setting up a PostgreSQL database.

  2. Specify Environment Variables: Update POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD with your preferred values.

docker-compose.yml
version: '3.8'

services:
  db:
    image: postgres
    deploy:
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
    environment:
      POSTGRES_DB: #your_prefered_db_name
      POSTGRES_USER: #your_prefered_username
      POSTGRES_PASSWORD: #your_prefered_password
    ports:
      - 5432:5432
    volumes:
      - db:/var/lib/postgresql/data

volumes:
  db:
    driver: local
  1. Run Docker Container:

  • To create and run the PostgreSQL container, run:

    docker-compose up -d
  • To stop and remove the container:

    docker-compose down
  • To stop, remove, and clear volumes:

    docker-compose down --volumes

Database Migrations with TypeORM

After setting up your Docker container, you can manage your database schema using TypeORM’s migration tools.

Main Commands

TypeORM supports several commands for managing database migrations, which are implemented as part of the project’s setup.

  • Generate a New Migration: Run this command when you modify any entities to keep the database in sync.

    yarn run migration:generate src/db/migrations/MigrationName
    • Example: yarn run migration:generate src/db/migrations/AddNewColumn

    • Make sure to specify the correct path and name your migration descriptively.

  • Run Migrations: Apply pending migrations to the database.

    yarn run migration:run
  • Seed the Database: Populate the database with initial or sample data.

    yarn run seed
    • Note: Seeders are executed in alphabetical order, so name them accordingly if they are dependent on each other.

Seeds

Seeds are predefined data sets used to populate the database with initial or sample data. For this application, seeds include essential predefined data such as user roles, team roles, and super admin credentials, which are crucial for initializing the system with baseline configurations.

Setup

  1. Run database migrations to ensure the database structure is up to date:

    yarn run migration:run
  2. Populate the database with initial data by running seeds:

    yarn seed

This will set up predefined configurations like user roles, team roles, and super admin credentials, preparing the database for use in development or production environments.


Verification

To verify everything is set up correctly:

  • Confirm Docker is Running: Check if the PostgreSQL service is running in Docker Desktop.

  • Access the Database: Connect to the PostgreSQL instance via a database client like pgAdmin or DataGrip using localhost:5432.

With these steps, you now have a fully Dockerized PostgreSQL setup along with commands for managing migrations and seeding data in a project.

Last updated