Quick Setup
The easiest way to configure your database is using individual environment variables:Database URI Priority
The platform checks for database configuration in this order:- DATABASE_URL (if set, overrides all other settings)
- Individual variables (DATABASE_TYPE, DB_USERNAME, etc.)
- Defaults (PostgreSQL on localhost)
Configuration Options
Option 1: Direct DATABASE_URL
Complete database connection URI. When set, this takes precedence over all other database settings.PostgreSQL format:SQLite format:
This is commonly set automatically by hosting platforms like Heroku, Railway, or Render.
Option 2: Individual Variables
Database engine to use.Supported values:
postgresql- Recommended for productionsqlite- Good for development and testing
Database user for authentication.Example:
Not used when
DATABASE_TYPE=sqliteDatabase password for authentication.Example:
Database host address.Examples:
Database name to connect to.Example:
Database Examples
PostgreSQL (Production)
Using DATABASE_URL:SQLite (Development)
Using DATABASE_URL:For SQLite, the database file is created in the project’s base directory (where
config.py lives).Testing Environment
The testing configuration automatically uses an in-memory SQLite database:Initializing the Database
Using Flask-Migrate (Recommended)
Flask-Migrate uses Alembic to manage database schema migrations:Using init_db.py (Development Only)
For quick development setup with sample data:Database Settings
The following SQLAlchemy settings are configured automatically:Disabled by default to save resources. The Flask-SQLAlchemy event system is not needed for this application.
Automatically generated from your environment variables using the
get_database_uri() method.Environment-specific behavior:- Development: Uses configured database (PostgreSQL or SQLite)
- Testing: Uses in-memory SQLite (
sqlite:///:memory:) - Production: Uses configured database (PostgreSQL recommended)
Production Recommendations
Best Practices
-
Use managed database services:
- AWS RDS
- Google Cloud SQL
- Azure Database for PostgreSQL
- Railway, Render, or Supabase
-
Set DATABASE_URL via platform secrets:
- Enable connection pooling for high-traffic applications
-
Run migrations before deployment:
- Set up regular backups of your production database
Troubleshooting
Connection Issues
If you can’t connect to PostgreSQL:Migration Issues
If migrations fail:SQLite Permissions
Ensure the application has write permissions to the database directory:Next Steps
Environment Variables
Configure environment-specific settings
Security Settings
Set up CSP, rate limiting, and HTTPS