Supabase PostgreSQL Setup Guide
Status: Active
Last Updated: 2025-11-02
Related Docs: D1 Manager Setup, D1 Credentials, Deployment
Code Location: backend/config/wrangler.toml
Overview
This guide walks through provisioning Supabase PostgreSQL database instances for the EPGOAT event matching system.
Prerequisites
1. Cloudflare Account Setup
- Account Required: You need an active Cloudflare account
- Sign up: https://dash.cloudflare.com/sign-up (if you don't have one)
- Free Tier: D1 is available on Cloudflare's free tier
2. Create API Token
You need a Cloudflare API token with D1 permissions:
- Navigate to: https://dash.cloudflare.com/profile/api-tokens
- Click "Create Token"
- Use template: "Edit Cloudflare Workers" (includes D1 access)
- Or create custom token with these permissions:
- Account → D1 → Edit
- Account → Workers Scripts → Edit
- Copy the generated token (you'll only see it once!)
3. Store API Token
Add to your .env file:
# Cloudflare Configuration
CLOUDFLARE_API_TOKEN=your_token_here
CLOUDFLARE_ACCOUNT_ID=your_account_id_here
Finding your Account ID: 1. Go to: https://dash.cloudflare.com/ 2. Select any Workers & Pages project (or create one) 3. Your Account ID is in the right sidebar
Step 1: Create Production Database
# Export token (or source .env)
export CLOUDFLARE_API_TOKEN="your_token_here"
# Create production database
npx supabase create epgoat-events-prod
Expected Output:
✅ Successfully created DB 'epgoat-events-prod'!
[[d1_databases]]
binding = "DB"
database_name = "epgoat-events-prod"
database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Important: Save the database_id from the output! You'll need it for wrangler.toml.
Step 2: Create Staging Database
# Create staging database
npx supabase create epgoat-events-staging
Expected Output:
✅ Successfully created DB 'epgoat-events-staging'!
[[d1_databases]]
binding = "DB"
database_name = "epgoat-events-staging"
database_id = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
Important: Save this database_id as well!
Step 3: Configure wrangler.toml
Create wrangler.toml in the project root with both database configurations:
name = "epgoat-linker"
main = "backend/epgoat/workers/index.js"
compatibility_date = "2025-10-30"
# Production Environment
[env.production]
[[env.production.d1_databases]]
binding = "DB"
database_name = "epgoat-events-prod"
database_id = "YOUR_PROD_DATABASE_ID_HERE"
# Staging Environment
[env.staging]
[[env.staging.d1_databases]]
binding = "DB"
database_name = "epgoat-events-staging"
database_id = "YOUR_STAGING_DATABASE_ID_HERE"
# Development uses local D1
[env.development]
[[env.development.d1_databases]]
binding = "DB"
database_name = "epgoat-events-dev"
database_id = "" # Local uses .wrangler/state/d1/
Replace:
- YOUR_PROD_DATABASE_ID_HERE with the production database ID from Step 1
- YOUR_STAGING_DATABASE_ID_HERE with the staging database ID from Step 2
Step 4: Test Connectivity
Test Production Database
# List all Supabase databases
npx supabase list
# Execute test query on production
npx supabase db execute epgoat-events-prod --command="SELECT 1 as test"
# Execute test query on staging
npx supabase db execute epgoat-events-staging --command="SELECT 1 as test"
Expected Output:
┌──────┐
│ test │
├──────┤
│ 1 │
└──────┘
Test Local Development Database
# Create local dev database
npx supabase create epgoat-events-dev --local
# Test local query
npx supabase db execute epgoat-events-dev --local --command="SELECT 1 as test"
Step 5: Update .env.example
Add Cloudflare configuration template to .env.example:
# Cloudflare Configuration (for Supabase database provisioning)
CLOUDFLARE_API_TOKEN=your_cloudflare_api_token_here
CLOUDFLARE_ACCOUNT_ID=your_cloudflare_account_id_here
Verification Checklist
- [ ] Cloudflare account created
- [ ] API token created with D1 permissions
- [ ] Token added to
.envfile - [ ] Production database created (
epgoat-events-prod) - [ ] Staging database created (
epgoat-events-staging) - [ ] Both database IDs saved
- [ ]
wrangler.tomlcreated with both environments - [ ] Connectivity tested for production
- [ ] Connectivity tested for staging
- [ ]
.env.exampleupdated with Cloudflare config template
Next Steps
Once all databases are provisioned and tested:
- ✅ Mark P1-001 as complete in PROJECT_API_MATCH_OVERHAUL.md
- → Proceed to P1-002: Create Database Schema (schema.sql with 18 tables)
- → Run migrations on both databases
Troubleshooting
Error: "CLOUDFLARE_API_TOKEN environment variable not set"
Solution: Export the token or source your .env file:
source .env
# OR
export CLOUDFLARE_API_TOKEN="your_token_here"
Error: "API token lacks required permissions"
Solution: Recreate the token with these permissions: - Account → D1 → Edit - Account → Workers Scripts → Edit
Error: "Database name already exists"
Solution: Either:
1. Use the existing database (get ID with npx supabase list)
2. Delete the old database: npx supabase delete DATABASE_NAME
3. Choose a different name
Database IDs Reference
Keep this for reference once created:
Production:
Name: epgoat-events-prod
ID: [PASTE_ID_HERE]
Staging:
Name: epgoat-events-staging
ID: [PASTE_ID_HERE]
Resources
- Supabase PostgreSQL Docs: https://developers.cloudflare.com/d1/
- Wrangler CLI Docs: https://developers.cloudflare.com/workers/wrangler/
- API Token Creation: https://developers.cloudflare.com/fundamentals/api/get-started/create-token/
- D1 Pricing: https://developers.cloudflare.com/d1/platform/pricing/ (Free tier: 5 GB storage, 5M reads/day)
Last Updated: 2025-10-30 Related Tasks: P1-002 (Create Schema), P1-003 (Migrations)