Spaces:
Running
Running
Supabase Setup Guide for Hugging Face Deployment
Why Supabase?
- Persistent Database: Data survives container restarts
- Production Ready: Scalable PostgreSQL database
- Free Tier: Generous free tier for development
- Easy Setup: Managed PostgreSQL with automatic backups
- Real-time: Built-in real-time subscriptions (if needed)
Step 1: Create Supabase Project
- Go to https://supabase.com
- Sign up/Login with GitHub
- Click "New Project"
- Choose organization and project name
- Set a strong database password
- Choose a region close to your users
- Wait for setup to complete (2-3 minutes)
Step 2: Get Database Connection Details
- In your Supabase dashboard, go to Settings → Database
- Find the Connection string section
- Copy the URI (it looks like:
postgresql://postgres:[password]@[host]:5432/postgres
)
Step 3: Configure Hugging Face Spaces
Option A: Using Hugging Face Spaces Environment Variables
- Go to your Hugging Face Space
- Click Settings → Repository secrets
- Add these secrets:
SUPABASE_DB_URL=postgresql+asyncpg://postgres:[password]@[host]:5432/postgres
ENVIRONMENT=production
Option B: Using .env file (for local testing)
Create a .env
file in your project root:
# Supabase Database
SUPABASE_DB_URL=postgresql+asyncpg://postgres:[password]@[host]:5432/postgres
ENVIRONMENT=production
# Other settings
OPENAI_API_KEY=your_openai_key_here
SECRET_KEY=your_secret_key_here
Step 4: Database Schema Setup
The application will automatically create all tables on startup. However, if you want to use Alembic for migrations:
- Install Alembic:
pip install alembic
- Initialize:
alembic init alembic
- Create migration:
alembic revision --autogenerate -m "Initial migration"
- Apply:
alembic upgrade head
Step 5: Test the Connection
The application will automatically:
- Connect to Supabase on startup
- Create all necessary tables
- Log the connection status
Environment Variables Reference
Variable | Description | Required |
---|---|---|
SUPABASE_DB_URL |
PostgreSQL connection string | Yes (for production) |
ENVIRONMENT |
Set to "production" for Supabase | Yes |
DATABASE_URL |
Fallback SQLite URL | No (for development) |
Troubleshooting
Connection Issues
- Verify the connection string format
- Check if the database password is correct
- Ensure the host is accessible from Hugging Face
Permission Issues
- Supabase automatically creates the
postgres
user with full permissions - No additional setup needed for basic operations
Performance Issues
- The connection pool is optimized for Hugging Face Spaces
- Pool size reduced to 5 connections to avoid limits
Security Notes
- Never commit database credentials to version control
- Use Hugging Face secrets for production
- Supabase automatically handles SSL connections
- Database is automatically backed up
Migration from SQLite
The application automatically detects the environment and switches to Supabase when:
ENVIRONMENT=production
is setSUPABASE_DB_URL
is provided
No code changes needed - it's handled automatically by the configuration system.