Skip to main content

Database Setup

WorkForce uses PostgreSQL with Prisma ORM for data management.

Installing PostgreSQL

Windows

  1. Download from postgresql.org
  2. Run the installer
  3. Set a password for the postgres user
  4. Default port: 5432

Ubuntu/Debian

sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresql

Docker

docker run -d \
--name workforce-db \
-e POSTGRES_USER=workforce \
-e POSTGRES_PASSWORD=your_password \
-e POSTGRES_DB=workforce \
-p 5432:5432 \
postgres:16

Creating the Database

-- Connect as postgres superuser
CREATE USER workforce WITH PASSWORD 'your_secure_password';
CREATE DATABASE workforce OWNER workforce;
GRANT ALL PRIVILEGES ON DATABASE workforce TO workforce;

Connection String

Format:

postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=public

Examples:

# Local development
DATABASE_URL="postgresql://workforce:password@localhost:5432/workforce?schema=public"

# Remote server
DATABASE_URL="postgresql://workforce:password@db.example.com:5432/workforce?schema=public"

# With SSL
DATABASE_URL="postgresql://workforce:password@db.example.com:5432/workforce?schema=public&sslmode=require"

Running Migrations

# Apply all migrations (production)
npx prisma migrate deploy

# Create a new migration (development)
npx prisma migrate dev --name description_of_change

# Reset database (WARNING: destroys all data)
npx prisma migrate reset

Prisma Schema

The database schema is defined in prisma/schema.prisma. Key models include:

ModelPurpose
TenantOrganization / company
EmployeeUser account with role
DepartmentOrganizational unit
ShiftWork schedule definition
AttendanceCheck-in/out records
RequestEmployee requests (leave, etc.)
ApprovalApproval records for requests
WorkflowConfigApproval workflow definitions
WorkflowStageIndividual stages in a workflow
OfficeLocationGeofence locations
SubscriptionTenant plan and billing

Prisma Studio

Browse and edit data visually:

npx prisma studio

Opens a web UI at http://localhost:5555.

Backups

Manual Backup

pg_dump -U workforce -d workforce -F c -f backup_$(date +%Y%m%d).dump

Restore

pg_restore -U workforce -d workforce -c backup_20240101.dump

Automated Backups

Set up a cron job (Linux) or Task Scheduler (Windows) to run pg_dump daily.