Database Architecture
The application uses a multi-database tenancy approach:- Central Database: Stores system-wide data (tenants, plans, subscriptions, domains)
- Tenant Databases: Each tenant gets their own PostgreSQL database for isolated data storage
This architecture provides complete data isolation between tenants and makes backup/restore operations per-tenant easier.
PostgreSQL Setup
Local Development
Install PostgreSQL on your system:Environment Configuration
Configure your database connection in.env:
The database driver to use. For this application, use
pgsql for PostgreSQL.The database server hostname or IP address.
The PostgreSQL server port.
The name of your central database.
Database username with permissions to create databases (required for tenant database creation).
Database password for authentication.
Character set for the database connection.
SSL mode for PostgreSQL connections. Options:
disable, allow, prefer, require, verify-ca, verify-fullExample Configuration
Running Migrations
Central Database Migrations
Run migrations for the central database (tenants, plans, subscriptions):tenants- Tenant information and metadatadomains- Custom domains for tenantsplans- Subscription planssubscriptions- Tenant subscription datafeatures- Available features for plansactivity_logs- System activity trackingsettings- Global application settings
Tenant Database Migrations
Create and migrate a tenant database:Tenant migrations are located in
database/migrations/tenant/ and run automatically when new tenants are created.Database Schema Overview
Central Database Tables
Tenants Table
Stores all tenant information:Domains Table
Manages custom domains for tenants:Plans Table
Defines available subscription plans:Subscriptions Table
Tracks tenant subscriptions:Features Table
Defines available features:Tenant Database Tables
Each tenant database contains:users- Tenant-specific userspassword_reset_tokens- Password reset tokenssessions- User sessionspersonal_access_tokens- API tokensjobs- Background job queuecache- Cache storagesettings- Tenant-specific settings
Tenancy Configuration
The multi-tenant behavior is configured inconfig/tenancy.php:
tenant{tenant_id}
For example, a tenant with ID abc123 gets database: tenantabc123
Database Management Commands
Tenant Operations
Database Maintenance
Production Considerations
Grant Database Creation Rights
Connection Pooling
For production, use connection pooling with PgBouncer:Performance Tuning
Optimize PostgreSQL for Laravel:Backup Strategy
- Central Database: Daily automated backups
- Tenant Databases: Per-tenant backup schedules
- Point-in-Time Recovery: Enable WAL archiving
Troubleshooting
Connection Issues
Permission Errors
Tenant Database Issues
Next Steps
- Configure email settings for tenant notifications
- Learn about tenant management features
- Set up monitoring and logging for database performance