Overview
Open Wearables uses PostgreSQL as its primary database and Alembic for schema migrations. This guide covers database setup, migration management, backup strategies, and troubleshooting.Database Requirements
PostgreSQL Version: 18+ (recommended), 15+ (minimum) Extensions Required:uuid-ossp- UUID generation functions (auto-enabled)
- Minimum RAM: 2GB
- Minimum Storage: 20GB (varies with data volume)
- Timezone: UTC (strongly recommended)
Quick Setup
Configure database connection
Set database credentials in See Environment Variables for production configuration.
backend/config/.env:.env
Start database service
Using Docker Compose:The database will be available on
localhost:5432 with health checks enabled.Database Schema
The Open Wearables database schema includes:Core Tables
| Table | Description |
|---|---|
users | User accounts and authentication |
api_keys | API keys for programmatic access |
invitations | User invitation management |
providers | Wearable provider definitions (Garmin, Polar, etc.) |
user_connections | OAuth connections to providers |
Data Tables
| Table | Description |
|---|---|
series_type_definitions | Standardized metric definitions |
workouts | Exercise activities and sessions |
workout_data_points | Time-series data within workouts |
sleep_sessions | Sleep tracking sessions |
sleep_data_points | Time-series sleep stage data |
heart_rate_samples | Heart rate measurements |
samples | Generic time-series data |
Background Processing
| Table | Description |
|---|---|
background_tasks | Celery task tracking |
sync_logs | Provider synchronization history |
The schema is designed to normalize data from multiple wearable providers into a consistent format using
series_type_definitions.Migration Management
Running Migrations
- Migrations run automatically on application startup
- Safe to run multiple times (idempotent)
- Applied in sequential order based on revision numbers
Creating Migrations
When you modify database models inapp/models/, create a migration:
Generate migration
Create an autogenerated migration:This creates a new file in
backend/migrations/versions/.Review migration
Always review the generated migration file:
migrations/versions/xxx_add_timezone_to_users.py
Migration Best Practices
Backward compatibility
Backward compatibility
Write migrations that can be rolled back:
Data migrations
Data migrations
For complex data transformations, separate schema and data changes:
Large table migrations
Large table migrations
For tables with millions of rows, use concurrent operations:
Concurrent operations require a database connection outside a transaction. Set
connection.execution_options(isolation_level="AUTOCOMMIT") if needed.Rolling Back Migrations
Migration History
Database Connection
Connection String Format
Open Wearables uses SQLAlchemy with the psycopg driver:Connection Pooling
SQLAlchemy manages connection pooling automatically with these defaults:- Pool size: 5 connections
- Max overflow: 10 additional connections
- Pool timeout: 30 seconds
- Pool recycle: 3600 seconds (1 hour)
backend/app/database.py
Testing Database Connection
Seed Data
Initial Data
On first startup, the application automatically creates:- Admin account - From
ADMIN_EMAILandADMIN_PASSWORDenvironment variables - Series type definitions - Standardized metric definitions for normalizing provider data
Sample Data (Development)
Load sample test data for development:- Sample users
- Example workouts with data points
- Sleep sessions with sleep stages
- Heart rate samples
- Provider connections
Custom Seed Scripts
Create custom seed scripts inbackend/scripts/:
backend/scripts/seed_custom.py
Backup and Restore
Creating Backups
Restoring Backups
Automated Backup Strategy
For production, implement automated backups:Cron-based backup script
Cron-based backup script
backup.sh
Managed database backups
Managed database backups
When using managed PostgreSQL services (AWS RDS, Azure Database, etc.):
- Enable automated daily backups
- Set retention period (7-35 days recommended)
- Configure backup window during low-traffic hours
- Enable point-in-time recovery (PITR)
- Test restore procedures regularly
- Automated backups: Enabled
- Backup retention: 30 days
- Backup window: 02:00-04:00 UTC
- PITR: Enabled (allows restore to any second in retention period)
Performance Optimization
Indexes
The schema includes indexes on frequently queried columns:Indexes speed up queries but slow down inserts/updates. Monitor query performance and add indexes based on actual usage patterns.
Monitoring Query Performance
Vacuuming and Maintenance
PostgreSQL requires periodic maintenance:Production Database
Managed Database Services
For production, use managed PostgreSQL services:AWS RDS
AWS RDS
Recommended configuration:
- Instance: db.t4g.medium or larger
- Storage: General Purpose SSD (gp3) with auto-scaling
- Multi-AZ: Enabled for high availability
- Encryption: Enabled at rest and in transit
- Automated backups: 30-day retention
- Monitoring: Enhanced monitoring enabled
.env
Azure Database for PostgreSQL
Azure Database for PostgreSQL
Recommended configuration:
- Tier: General Purpose
- Compute: 2-4 vCores
- Storage: 100GB with auto-growth
- Backup retention: 30 days
- High availability: Zone-redundant
- SSL enforcement: Enabled
.env
Google Cloud SQL
Google Cloud SQL
Recommended configuration:
- Machine type: db-custom-2-7680 (2 vCPU, 7.5GB RAM)
- Storage: 100GB SSD with auto-increase
- Availability: Regional (high availability)
- Backups: Automated daily with 30-day retention
- Encryption: Enabled
.env
Security Best Practices
Troubleshooting
Connection refused errors
Connection refused errors
Symptoms:
psycopg.OperationalError: connection refusedSolutions:-
Verify database container is running:
-
Check database logs:
-
Verify connection settings in
.env: -
Test connection manually:
Migration errors
Migration errors
Symptoms:
alembic.util.exc.CommandError or migration failuresSolutions:-
Check current migration version:
-
View migration history:
-
If migrations are out of sync, stamp the database:
-
For conflicts, manually resolve or rollback:
Slow queries
Slow queries
Symptoms: API responses are slow, database CPU is highDiagnosis:
-
Enable slow query logging:
-
View running queries:
-
Check for missing indexes:
- Add indexes on frequently filtered columns
- Optimize queries to reduce data scanned
- Use pagination for large result sets
- Consider database scaling if consistently slow
Disk space issues
Disk space issues
Symptoms:
No space left on device errorsSolutions:-
Check volume usage:
-
Check database size:
-
Clean up old data:
- Increase volume size or enable auto-scaling in managed services
Next Steps
Docker Deployment
Complete deployment guide with Docker Compose
Environment Variables
Configure all environment settings
