Database & Migrations¶
PostgreSQL database with Alembic migration management.
Quick Start¶
| Bash | |
|---|---|
| |
Database Setup¶
Start Database¶
| Bash | |
|---|---|
Database accessible at:
- Host:
localhost - Port:
54323 - Database:
craftyourstartup - Username:
craftyourstartup - Password:
craftyourstartup
Connect to Database¶
Using psql:
| Bash | |
|---|---|
Using GUI (DBeaver recommended):
- Host: localhost
- Port: 54323
- Database: craftyourstartup
- Username: craftyourstartup
- Password: craftyourstartup
Database Models¶
All Tiers¶
User:
- id (UUID)
- email (unique)
- password_hash
- full_name
- created_at, last_login
- verified, is_superuser
- articles (relationship)
Article:
- id (int)
- title, content, author
- published_at, is_published
- user_id (foreign key)
Top G & AI Velocity Only¶
Purchase (one-time payments):
- id, user_id
- product_type, price_id
- transaction_id (Stripe)
- amount, currency
- purchase_date, is_successful
Subscription (recurring payments):
- id, user_id
- stripe_subscription_id
- plan, status
- start_date, end_date
Migrations¶
Create Migration¶
After modifying models in app/models.py:
| Bash | |
|---|---|
Apply Migrations¶
Check Migration Status¶
```bash
Current version¶
task db:migrate-current
Migration history¶
task db:migrate-history
Full status¶
task db:migrate-status
| Text Only | |
|---|---|
Manual Alembic Commands¶
If you need direct Alembic access:
| Bash | |
|---|---|
Migration Workflow¶
1. Modify Model¶
| Python | |
|---|---|
2. Create Migration¶
```bash task db:migrate-create -- "add avatar_url to user"
| Text Only | |
|---|---|
4. Apply Migration¶
```bash task db:migrate-up
| Text Only | |
|---|---|
6. Generate API Client¶
| Bash | |
|---|---|
Common Issues¶
Database connection refused¶
```bash
Check Docker is running¶
docker ps
Restart database¶
docker-compose down docker-compose up -d
Wait 10 seconds¶
task db:migrate-up
| Text Only | |
|---|---|
Duplicate migration¶
| Bash | |
|---|---|
Can't connect with psql¶
```bash
Ensure using correct port¶
psql -h localhost -p 54323 -U craftyourstartup
Check db_port in local.env matches Docker Compose¶
Apply Migrations¶
| Bash | |
|---|---|
Or:
| Bash | |
|---|---|
Backup Before Migrations¶
| Bash | |
|---|---|
Database Tools¶
Recommended:
For inspection:
Learning Resources¶
Learn FastAPI & Databases: Free interactive tutorials covering FastAPI with PostgreSQL.
Related Documentation¶
- Configuration - Database connection settings
- Installation - Initial setup
- Development - Development workflows
- Alembic Documentation