FastAPI backend application with PostgreSQL/Supabase database.
-
Install Poetry (if not already installed):
curl -sSL https://install.python-poetry.org | python3 - -
Install dependencies:
poetry install
-
Configure environment variables:
cp .env.example .env
Edit
.envand set your configuration:SECRET_KEY: Required for JWT token signing (use a strong random string)DATABASE_URL: Your Supabase connection string or local PostgreSQL URL
- Create a Supabase project at supabase.com
- Go to Project Settings > Database
- Copy the "Connection string" with Connection pooling enabled (port 6543)
- Set it in your
.envfile:DATABASE_URL=postgresql://postgres.[PROJECT-REF]:[PASSWORD]@aws-0-[REGION].pooler.supabase.com:6543/postgres
Important Supabase Notes:
- Always use the connection pooler (port 6543) instead of direct connection (port 5432)
- The app is optimized with smaller connection pool sizes since Supabase has its own pooler
- Connection pool configuration: 5 connections, max overflow 10
- Connections are recycled every 5 minutes
- Automatic connection health checks enabled (
pool_pre_ping=True)
If DATABASE_URL is not set, the app falls back to:
postgresql://postgres:root@localhost:5432/py-db
This project uses Alembic for database migrations:
# Create a new migration after schema changes
alembic revision --autogenerate -m "description of changes"
# Apply migrations to database
alembic upgrade head
# Check current migration status
alembic current
# View migration history
alembic historyImportant: Never create tables manually with Base.metadata.create_all(). Always use Alembic migrations.
poetry run fastapi dev src/app/main.pyThe application will run on http://localhost:8000 by default.
- API Documentation:
http://localhost:8000/docs - Health Check:
http://localhost:8000/health(includes database connectivity test)
src/app/
├── routes/ # API endpoints (FastAPI routers)
├── services/ # Business logic and database operations
├── models/ # SQLAlchemy ORM models
├── schemas.py # Pydantic models for validation
├── dependencies.py # FastAPI dependencies (auth, DB sessions)
├── database.py # Database connection and configuration
└── utils/ # Custom exceptions and utilities
- The app uses SQLAlchemy's QueuePool with optimized settings for Supabase
- Connections are validated before use (
pool_pre_ping=True) - Automatic connection recycling prevents stale connections
- TCP keepalive configured for better connection stability
- Database errors are caught and logged in
get_db()dependency - Custom exceptions for authentication and resource errors
- Global exception handlers for consistent API responses
- JWT-based authentication with access and refresh tokens
- Access tokens expire after 60 minutes (configurable)
- Refresh tokens expire after 7 days (configurable)
- Passwords hashed with bcrypt
- Protected endpoints use
get_current_userdependency
-
Check connectivity:
curl http://localhost:8000/health
Should return:
{"status": "healthy", "database": "connected", "api": "ok"} -
Verify DATABASE_URL:
- Ensure it's using the connection pooler (port 6543)
- Check credentials are correct
- Verify Supabase project is not paused
-
Check logs: The application logs database connection events and errors
If migrations fail:
# Check current state
alembic current
# View pending migrations
alembic history
# Reset to a specific version
alembic downgrade <revision>Required:
SECRET_KEY- JWT signing key (must be set)DATABASE_URL- Database connection string (optional, falls back to local)
Optional with defaults:
ALGORITHM- JWT algorithm (default: HS256)MINUTES_TOKEN_EXPIRE- Access token TTL (default: 60)DAYS_REFRESH_TOKEN_EXPIRE- Refresh token TTL (default: 7)
See .env.example for complete configuration template.