Troubleshooting
Common issues and their solutions when working with the FastAPI Template.
Database Issues
Database Connection Refused
Error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Solutions:
-
Check PostgreSQL is running:
# Linux/Mac sudo systemctl status postgresql # Using Docker docker ps | grep postgres -
Verify database exists:
psql -U postgres -l # If missing, create it: psql -U postgres CREATE DATABASE fastapi_template; -
Check
.envsettings match your database: DB_USER,DB_PASSWORDmust match PostgreSQL credentialsDB_ADDRESSshould belocalhostfor local devDB_PORTshould be5432(default PostgreSQL port)
Migration Errors
Error:
alembic.util.exc.CommandError: Can't locate revision identified by 'xxxxx'
Solution: Your migration history is out of sync. For development:
# Clear and reinitialize (WARNING: Deletes all data)
api-admin db init --force
For production, see Database Migration Strategy.
Application Startup Issues
Port Already in Use
Error:
OSError: [Errno 48] Address already in use
Solutions:
-
Find process using port 8000:
# Linux/Mac lsof -i :8000 # Kill the process kill -9 <PID> -
Use a different port:
api-admin serve --port 8080
Module Not Found Errors
Error:
ModuleNotFoundError: No module named 'app'
Solutions:
-
Activate virtual environment:
# Using uv source .venv/bin/activate # Linux/Mac .venv\Scripts\activate # Windows # Using pip/venv source venv/bin/activate # Linux/Mac venv\Scripts\activate # Windows -
Reinstall dependencies:
uv sync # or pip install -r requirements.txt
Email Issues
Password Reset Not Sending Emails
Problem: Clicked "forgot password" but no email arrives.
Solutions:
- Check email is configured in
.env: - All
MAIL_*variables must be set -
Test with:
MAIL_SERVER,MAIL_PORT,MAIL_USERNAME,MAIL_PASSWORD -
Check spam folder - emails may be flagged as spam
-
Verify SMTP credentials:
# Test connection manually telnet smtp.gmail.com 587 -
For development: Check application logs at
LOG_PATHfor email errors
Email Settings Valid But Still Not Working
Common issues:
- Gmail: Requires "App Password" not your regular password
- Office365: May require OAuth2 instead of SMTP
- 2FA enabled: Need app-specific password
- Check
MAIL_STARTTLSandMAIL_SSL_TLSsettings match your provider
Authentication Issues
"That token is Invalid" on Every Request
Causes:
- SECRET_KEY changed - All existing tokens invalidated
- Expected after changing SECRET_KEY
-
Users must login again
-
Clock skew - Server time incorrect
# Check server time date -
Token expired - Default 2 hours
- Use refresh token to get new access token
- See Authentication Flow
Cannot Access Admin Panel
Error: 404 Not Found on /admin
Solutions:
-
Check admin panel is enabled in
.env:ADMIN_PAGES_ENABLED=True -
Verify route in settings (if customized):
ADMIN_PAGES_ROUTE=/admin -
Restart application after changing settings
Metrics Issues
/metrics Returns 404
Cause: Metrics not enabled.
Solution: Add to .env:
METRICS_ENABLED=true
Restart the application.
Prometheus Not Scraping Metrics
Solutions:
-
Check
/metricsendpoint is accessible:curl http://localhost:8000/metrics -
Verify Prometheus configuration has correct target:
scrape_configs: - job_name: 'fastapi-template' static_configs: - targets: ['localhost:8000'] -
Check firewall isn't blocking Prometheus
Performance Issues
Application Running Slowly
Common causes:
-
DEBUG logging in production:
LOG_LEVEL=INFO # or WARNING, not DEBUG -
Database not indexed - Check slow query logs
-
No connection pooling - Consider adding pgbouncer for production
High Memory Usage
Solutions:
-
Check log retention:
LOG_RETENTION=7 days # Don't keep logs forever LOG_COMPRESSION=zip # Compress rotated logs -
Review metrics collection - May need sampling for high-traffic apps
Development Issues
Tests Failing
Error: Database connection errors in tests
Solutions:
-
Ensure test database exists:
psql -U postgres CREATE DATABASE fastapi_template_tests; -
Check
TEST_DB_NAMEin.envmatches test database -
Run tests with pytest:
pytest # or poe test
Pre-commit Hooks Failing
Solutions:
-
Install prek hooks:
prek install -
Update hooks:
prek autoupdate -
Skip hooks temporarily (not recommended):
git commit --no-verify
Still Having Issues?
If your problem isn't listed here:
- Check application logs at
LOG_PATH(default:./logs/api.log) - Enable DEBUG logging temporarily:
LOG_LEVEL=DEBUG LOG_CATEGORIES=ALL - Review the configuration guide for all settings
- Open an issue on GitHub with:
- Error message (full traceback)
- Relevant log entries
- Your environment (OS, Python version)
- Steps to reproduce