Skip to main content

Overview

CompanyFlow uses PostgreSQL 14 or higher as its primary database. The application uses the pgx/v5 driver for database connectivity and includes a custom SQL-based migration runner.

Prerequisites

Before setting up the database, ensure you have:
  • PostgreSQL 14 or higher installed
  • Database user with appropriate privileges
  • Network access to the PostgreSQL server

Installation

1

Install PostgreSQL

Install PostgreSQL on your system:
2

Create Database

Create a new database for CompanyFlow:
For production environments, use a strong password and limit privileges to only what’s necessary.
3

Enable Required Extensions

CompanyFlow requires the pgcrypto extension for UUID generation:
This extension is automatically enabled when you run migrations, but you can enable it manually if needed.
4

Verify Connection

Test your database connection:
If successful, you should see the PostgreSQL prompt.

Database Configuration

Connection String Format

CompanyFlow uses the following connection string format:

SSL Mode Options

string
default:"disable"
Controls SSL/TLS encryption for the database connection:
  • disable - No SSL (development only)
  • require - SSL required, no certificate verification
  • verify-ca - SSL required, verify CA certificate
  • verify-full - SSL required, verify CA and hostname
Always use sslmode=require or higher in production environments to encrypt database traffic.

Connection Pooling

CompanyFlow uses pgxpool for connection pooling. The default pool settings are managed by the pgx library, which typically creates:
  • Maximum connections: Based on system resources
  • Minimum connections: 0 (connections created on demand)
  • Connection lifetime: Automatic cleanup of idle connections

Custom Pool Configuration

To customize connection pool settings, you can modify the connection string in config/config.go:27:

Schema Overview

The CompanyFlow database includes the following main tables:
  • companies - Multi-tenant company records
  • tenants - Subscription and plan information
  • employees - Employee records with authentication
  • departments - Organizational departments
  • designations - Job titles and positions
  • levels - Employee hierarchy levels
  • roles - RBAC roles
  • permissions - Role-based permissions
  • leaves - Leave request records
  • memos - Internal communications
  • approvals - Approval workflow records
  • audit_logs - System activity tracking
  • schema_migrations - Migration tracking table

Testing Database Setup

Create Test Database

For running tests, create a separate test database:

Configure Test Environment

Set test database variables in your environment or .env file:

Troubleshooting

Connection Refused

If you see “connection refused” errors:
  1. Check if PostgreSQL is running:
  2. Verify PostgreSQL is listening on the correct port:

Authentication Failed

If authentication fails:
  1. Check pg_hba.conf configuration file
  2. Ensure password authentication is enabled for your connection type
  3. Verify username and password are correct

Permission Denied

If you see permission errors:

Next Steps

After setting up your database:
  1. Configure environment variables
  2. Run database migrations
  3. Test your API setup