Database Setup

This guide covers PostgreSQL database configuration, migration from file-based storage, and hybrid storage validation.

PostgreSQL Installation

docker compose up -d postgres

Option 2: Local PostgreSQL

macOS

brew install postgresql@15
brew services start postgresql@15

Ubuntu/Debian

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

Create Database and User

-- Connect as postgres user
sudo -u postgres psql

-- Create database
CREATE DATABASE ansibleinspec;

-- Create user
CREATE USER ansibleinspec WITH PASSWORD 'your_secure_password';

-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE ansibleinspec TO ansibleinspec;

-- Exit
\q

Configuration

Environment Variables

Connection String Format

Async (asyncpg): Used by application

Sync (psycopg2): Used by Alembic migrations

Database Migrations with Alembic

Initialize Alembic

Configure Alembic

Edit alembic/env.py:

Create Initial Migration

Apply Migrations

View Migration History

Hybrid Storage Configuration

Enable Hybrid Mode

This runs both file and database storage simultaneously for validation.

Storage Backends

  • file: File-based storage only (default, existing behavior)

  • database: PostgreSQL storage only

  • hybrid: Dual-write to both file and database

Migration from File Storage

Step 1: Backup Existing Data

Step 2: Initialize Database

Step 3: Migrate Data

Create scripts/migrate_file_to_db.py:

Step 4: Enable Hybrid Mode

Step 5: Monitor Validation Period

Access Prometheus metrics:

Key metrics to monitor:

  • storage_operations_total: Operation counts by backend

  • storage_latency_seconds: Operation latency

  • storage_consistency_errors_total: Data consistency errors

Monitoring and Validation

Check Consistency

The system automatically compares file and database storage on every read operation during hybrid mode.

View Validation Status

Response:

Cutover Criteria

System is ready for cutover when:

  1. Validation period complete (30 days)

  2. Consistency error rate < 1%

  3. Database error rate < 5%

  4. Database write latency < 100ms

Cutover to Database-Only

Manual Cutover

Automatic Cutover

System will automatically switch to database-only mode when criteria are met.

Rollback to File Storage

If issues occur:

Performance Tuning

Connection Pooling

Indexes

Key indexes are created automatically:

  • job_templates.name

  • job_templates.created_at

  • jobs.status

  • jobs.created_at

  • users.username

Query Optimization

Monitor slow queries:

Backup and Recovery

Automated Backups

Restore from Backup

Troubleshooting

Connection Refused

  • Check PostgreSQL is running: pg_isready

  • Verify connection string in .env

  • Check firewall rules

Permission Denied

  • Grant database privileges to user

  • Check pg_hba.conf authentication settings

Migration Failures

  • Check Alembic version compatibility

  • Review migration SQL: alembic upgrade head --sql

  • Apply migrations manually if needed

High Latency

  • Increase connection pool size

  • Add database indexes

  • Check database server resources

  • Consider read replicas for high load

Security Best Practices

  1. Use strong passwords (20+ characters, mixed case, numbers, symbols)

  2. Restrict network access (bind to localhost or private network)

  3. Enable SSL/TLS for connections

  4. Regular security updates for PostgreSQL

  5. Encrypt backups before storing

  6. Use secrets management (vault, AWS Secrets Manager)

  7. Monitor access logs for suspicious activity

References

Last updated