Database Setup
This page is dedicated to db setup via docker
Prerequisites
Install Docker Desktop: Download and install Docker Desktop.
Verify Docker Installation: Ensure Docker is running and verify with
docker --version
in your terminal.
Docker Configuration
Use a Docker Compose File: Below is a
docker-compose.yml
for setting up a PostgreSQL database.Specify Environment Variables: Update
POSTGRES_DB
,POSTGRES_USER
, andPOSTGRES_PASSWORD
with your preferred values.
Run Docker Container:
To create and run the PostgreSQL container, run:
To stop and remove the container:
To stop, remove, and clear 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.
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.
Seed the Database: Populate the database with initial or sample data.
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
Run database migrations to ensure the database structure is up to date:
Populate the database with initial data by running seeds:
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