Skip to content

Latest commit

 

History

History
135 lines (100 loc) · 4.1 KB

File metadata and controls

135 lines (100 loc) · 4.1 KB

Supabase Integration Guide

This guide walks you through connecting your Agriculture Drone GCS to Supabase (managed PostgreSQL).

Step 1: Create Supabase Project

  1. Go to https://app.supabase.com
  2. Sign up or log in
  3. Click "New Project"
  4. Choose organization, enter project name
  5. Set a strong database password (save this!)
  6. Choose region closest to your users
  7. Wait for project to initialize (~2 minutes)

Step 2: Get Database Connection String

  1. In your Supabase project dashboard, go to SettingsDatabase
  2. Find the "Connection info" section
  3. Copy the Connection string (URI format)
  4. It looks like: postgresql://postgres:PASSWORD@db.xxx.supabase.co:5432/postgres

Step 3: Configure Backend Environment

  1. Copy the environment template:

    cd backend
    copy .env.example .env
  2. Edit backend/.env and update:

    DATABASE_URL=postgresql://postgres:YOUR_PASSWORD@db.YOUR_PROJECT_ID.supabase.co:5432/postgres?sslmode=require
    SECRET_KEY=generate-a-long-random-secret-key-here
    

    Important:

    • Replace YOUR_PASSWORD with your Supabase database password
    • Replace YOUR_PROJECT_ID with your actual project ID from the connection string
    • Generate a strong SECRET_KEY (use a password generator for 32+ characters)

Step 4: Install PostgreSQL Driver

cd backend
venv\Scripts\activate
pip install -r requirements.txt

The requirements.txt now includes psycopg2-binary for PostgreSQL support.

Step 5: Create Database Tables

cd backend
venv\Scripts\activate
python -c "from database import create_tables; create_tables(); print('✅ Tables created successfully')"

You can verify tables were created by:

  1. Going to your Supabase project
  2. Click "Table Editor" in the sidebar
  3. You should see tables: users, fields, missions, waypoints, etc.

Step 6: Migrate Existing Data (Optional)

If you have existing users/data in your local SQLite database:

cd backend
venv\Scripts\activate
python migrate_to_supabase.py

This will copy all your existing users, fields, missions, and telemetry data to Supabase.

Step 7: Test the Connection

  1. Start your backend:

    cd backend
    venv\Scripts\activate
    python main.py
  2. You should see: "Connected to database successfully" (or similar)

  3. Test registration/login:

    • Start frontend: cd frontend && npm start
    • Try creating a new account
    • Check Supabase Table Editor → users table to verify the account was created

Step 8: Update CORS for Production

If deploying to production, update ALLOWED_ORIGINS in your .env:

ALLOWED_ORIGINS=http://localhost:3000,https://your-frontend-domain.com

Troubleshooting

Connection Failed

  • Verify your DATABASE_URL is correct
  • Ensure ?sslmode=require is at the end of the connection string
  • Check that your Supabase project is active (not paused)

SSL Certificate Error

Add ?sslmode=require to your DATABASE_URL if not already present.

Tables Not Created

  • Check the console output for error messages
  • Verify your connection string has the correct password
  • Ensure your Supabase project is fully initialized

Migration Issues

  • Ensure your local SQLite file exists (agriculture_gcs.db)
  • Check that DATABASE_URL points to Supabase (not SQLite)
  • Run migration script with error output: python migrate_to_supabase.py

Security Notes

  • Never commit .env files - they contain sensitive credentials
  • Use strong passwords for both Supabase and JWT secret keys
  • Supabase provides built-in security (SSL, connection limits)
  • Consider enabling Row Level Security (RLS) in Supabase for additional protection

Next Steps

Once connected to Supabase:

  1. Your app data is now shared across all devices
  2. Users can log in from anywhere
  3. Data persists even if your local machine is offline
  4. You can deploy your backend to any hosting service
  5. Consider using Supabase's built-in Auth system for additional features

For deployment, set the same environment variables on your hosting platform (Heroku, Render, Railway, etc.)