Skip to content

Database Troubleshooting

This comprehensive troubleshooting guide helps you diagnose and resolve issues with the database switching system. Whether you’re dealing with connection failures, switching problems, or configuration errors, this guide provides step-by-step solutions.

Start with these commands to understand your current system state:

🔍 System Status

Terminal window
npm run db:status

Shows current configuration, active provider, and available options.

🔗 Connection Test

Terminal window
npm run db:manage test

Tests connectivity to all configured database providers.

💊 Health Check

Terminal window
npm run db:manage health

Runs comprehensive diagnostics and performance checks.

🚨 Emergency Recovery

Terminal window
npm run db:restore

Restores previous configuration if switching failed.

This error appears when the system can’t find valid database credentials.

Symptoms:

  • API endpoints return 503 Service Unavailable errors
  • Dashboard shows “Database service unavailable”
  • npm run db:status shows “❌ No database providers configured”
  1. Check what’s missing:

    Terminal window
    npm run db:status
  2. Run the setup wizard:

    Terminal window
    npm run db:wizard
  3. Follow the interactive prompts to configure your database

Database operations timeout or fail to connect to the server.

Symptoms:

  • Database operations timeout
  • “Failed to connect” errors in logs
  • API endpoints return 500 Internal Server errors
  1. Test connectivity to both providers:

    Terminal window
    npm run db:manage test --verbose
  2. Check network connectivity (Supabase):

    Terminal window
    curl -I https://your-project.supabase.co
  3. Test from different network if possible

Auto-detection doesn’t select the expected database provider.

Symptoms:

  • Wrong database provider is selected automatically
  • Inconsistent behavior between environments
  • npm run db:status shows unexpected active provider

Solutions:

  1. Set Explicit Provider (Recommended):

    Terminal window
    # Set explicit provider in .env
    echo "DATABASE_PROVIDER=turso" >> .env
    # or
    echo "DATABASE_PROVIDER=supabase" >> .env
  2. Clear Auto-Detection Issues:

    Terminal window
    # Remove auto-detection, use explicit choice
    sed -i 's/DATABASE_PROVIDER=auto/DATABASE_PROVIDER=turso/' .env
  3. Understand Priority Logic:

    • Priority: Explicit choice → Supabase → Turso → Error
    • Multiple providers? Set DATABASE_PROVIDER to override

Operations succeed but data doesn’t match expectations.

Symptoms:

  • Missing tables or columns
  • Type conversion errors
  • Inconsistent data between providers
Terminal window
npm run db:schema
# Shows detailed schema comparison between providers

Invalid URL Format

Problem: TURSO_DATABASE_URL has wrong format

Fix:

# ✅ Correct format:
TURSO_DATABASE_URL=libsql://database-name.turso.io
# ❌ Wrong format:
TURSO_DATABASE_URL=https://database-name.turso.io

Database Not Found

Problem: Database doesn’t exist in Turso

Fix:

  • Verify database exists in Turso dashboard
  • Check organization and database name spelling
  • Ensure you have access permissions

Invalid API Key

Problem: Using wrong key type for operations

Fix:

# ❌ Don't use anon key for server operations
SUPABASE_ANON_KEY=eyJ...
# ✅ Use service role for server operations
SUPABASE_SERVICE_ROLE_KEY=eyJ...

RLS Policy Violation

Problem: Row Level Security blocks operations

Fix:

  • Check RLS policies in Supabase Dashboard
  • Ensure service role can access required tables
  • Review policy conditions for your use case

Project Paused

Problem: Free tier project paused due to inactivity

Fix:

  • Visit Supabase Dashboard
  • Unpause project in Settings
  • Consider upgrading plan if needed frequently

Switching command fails before making configuration changes.

Symptoms:

  • Switching stops with “backup creation failed”
  • .env.backup file is not created
  • File permission errors
  1. Check filesystem permissions:

    Terminal window
    ls -la .env
    ls -la .env.backup 2>/dev/null || echo "Backup doesn't exist"
  2. Fix permissions if needed:

    Terminal window
    chmod 644 .env
  3. Retry switching operation

Switching appears successful but database doesn’t change.

Diagnosis Steps:

  1. Check if .env file is writable:

    Terminal window
    ls -la .env
  2. Verify current provider:

    Terminal window
    npm run db:status | grep "Active Provider"
  3. Check file contents:

    Terminal window
    cat .env | grep DATABASE_PROVIDER

Solutions:

Terminal window
# Make .env writable
chmod 644 .env
# Retry switching operation
npm run db:switch:turso # or supabase

Switch completed but new database configuration doesn’t work.

  1. Immediate Rollback:

    Terminal window
    npm run db:restore
  2. Verify Restoration:

    Terminal window
    npm run db:status
  3. Manual Rollback (if automatic fails):

    Terminal window
    cp .env.backup .env
    npm run db:status # Verify restoration
  4. Diagnose Original Issue:

    Terminal window
    # Test the problematic configuration
    npm run db:manage test
    npm run db:manage health --verbose

Symptoms:

  • Import errors for database modules
  • TypeScript compilation fails
  • “Cannot find module” errors

Solutions:

  1. Clear Node modules and package lock:

    Terminal window
    rm -rf node_modules package-lock.json
  2. Reinstall dependencies:

    Terminal window
    npm install
  3. Check TypeScript configuration:

    Terminal window
    npm run type-check

Database configuration changes don’t take effect without server restart.

Expected Behavior: The system supports real-time provider switching without server restart.

Troubleshooting Steps:

  1. Check Current Provider in real-time:

    Terminal window
    # This should show changes immediately
    npm run db:status
  2. Test API Endpoint for real-time switching:

    Terminal window
    curl http://localhost:4321/api/supabase-test
    # Response should show current active provider
  3. Force Restart if needed:

    Terminal window
    # Kill existing processes
    pkill -f "npm run"
    # Start fresh
    npm run start

Symptoms:

  • Works locally but fails in production
  • Different database provider selected in different environments
  1. Check Production Environment Variables:

    • Ensure DATABASE_PROVIDER is set explicitly
    • Verify all required credentials are present
    • Use same credential format as local
  2. Set Explicit Provider in production:

    DATABASE_PROVIDER=supabase # or turso
  3. Validate Network Access:

    • Test database connectivity from production environment
    • Check firewall rules and security groups

API endpoints report incorrect or inconsistent provider information.

Diagnosis:

  1. Test API Endpoint:

    Terminal window
    curl http://localhost:4321/api/supabase-test
  2. Check System Status:

    Terminal window
    npm run db:status
  3. Compare Results: API response should match system status

Solutions:

  • Clear any cached environment variables
  • Restart development server completely
  • Check for multiple .env files in different locations

Symptoms:

  • No errors but operations don’t work
  • Empty results from database queries
  • API returns success but no data changes
  1. Test Direct Database Connection:

    Terminal window
    npm run db:manage test
  2. Check API Logs in development:

    Terminal window
    npm run dev
    # Look for console errors in terminal
  3. Verify Database Tables:

    Terminal window
    npm run db:manage tables
  4. Check Permissions (Supabase):

    • Review RLS policies in dashboard
    • Verify service role permissions

Symptoms:

  • API endpoints timeout frequently
  • Dashboard takes long time to load
  • Database queries are slower than expected
  1. Run Health Check with timing:

    Terminal window
    npm run db:manage health --verbose
  2. Check Network Latency:

    • Test connection speed to database
    • Consider geographic distance to database server
  3. Compare Providers:

    Terminal window
    # Switch and compare performance
    npm run db:switch:turso
    # Test operations...
    npm run db:switch:supabase
    # Test same operations...

Symptoms:

  • Switching process crashes
  • Out of memory errors during database operations

Solutions:

  1. Use Dry-Run Mode first:

    Terminal window
    node scripts/switch-database.js --to turso --dry-run
  2. Stop Other Processes before switching:

    Terminal window
    # Stop dev server and other resource-intensive processes
    pkill -f "npm run"
  3. Switch Without Other Operations running:

    Terminal window
    npm run db:switch:turso
    # Then restart other processes
    npm run start

Symptoms:

  • Cannot read or write .env files
  • Script execution fails with permission errors
  1. Fix File Permissions:

    Terminal window
    chmod 644 .env .env.backup
    chmod +x scripts/*.js
  2. Check Directory Permissions:

    Terminal window
    ls -la .
    # Verify you can read/write in current directory
  3. Fix Ownership Issues (if needed):

    Terminal window
    # Only if files are owned by different user
    sudo chown $USER:$USER .env .env.backup

Problem: Git wants to commit .env.backup files, causing merge conflicts.

  1. Ensure Backup Files are Ignored:

    Terminal window
    echo ".env.backup*" >> .gitignore
    echo ".env.*.backup" >> .gitignore
  2. Remove from Git if already tracked:

    Terminal window
    git rm --cached .env.backup
    git rm --cached .env.*.backup
  3. Clean Up Repository:

    Terminal window
    git add .gitignore
    git commit -m "Add .env backup files to .gitignore"

For detailed troubleshooting information:

Terminal window
# Enable debug logging for all database operations
DEBUG=1 npm run db:manage test
DEBUG=1 npm run db:switch:turso

Test database connections directly outside the abstraction layer:

Terminal window
node -e "
import { createClient } from '@libsql/client';
const client = createClient({
url: process.env.TURSO_DATABASE_URL,
authToken: process.env.TURSO_AUTH_TOKEN
});
console.log(await client.execute('SELECT 1 as test'));
"

Check for hidden characters or configuration issues:

Terminal window
# Check all database-related environment variables
env | grep -E "(DATABASE|TURSO|SUPABASE)" | sort
# Check for invisible characters or extra spaces
od -c .env | grep -E "(TURSO|SUPABASE|DATABASE)"
# Validate .env file format
npm run db:manage validate-config

If all else fails, reset the entire database configuration:

  1. Backup Current State:

    Terminal window
    cp .env .env.emergency-backup
    cp .env.backup .env.backup.emergency 2>/dev/null || true
  2. Run Fresh Setup:

    Terminal window
    npm run db:wizard
    # Follow prompts to reconfigure from scratch
  3. Test New Configuration:

    Terminal window
    npm run db:status
    npm run db:manage test
  4. Restore if Needed:

    Terminal window
    # If new setup doesn't work
    cp .env.emergency-backup .env

When reporting issues or asking for help, collect this information:

Terminal window
# Create comprehensive diagnostic report
{
echo "=== System Information ==="
echo "Node version: $(node --version)"
echo "NPM version: $(npm --version)"
echo "OS: $(uname -a)"
echo
echo "=== Database Status ==="
npm run db:status
echo
echo "=== Health Check ==="
npm run db:manage health
echo
echo "=== Environment Variables ==="
env | grep -E "(DATABASE|TURSO|SUPABASE)" | sed 's/=.*/=***/' | sort
} > debug-report.txt

Add these to your development routine:

Terminal window
# Weekly health check
npm run db:status
npm run db:manage health
# Before major changes
npm run db:backup
npm run db:schema
  1. Always Backup before experiments:

    Terminal window
    npm run db:backup
  2. Test Changes with dry-run first:

    Terminal window
    node scripts/switch-database.js --to turso --dry-run
  3. Validate Configuration after changes:

    Terminal window
    npm run db:schema
    npm run db:manage test
  4. Keep Emergency Backup:

    Terminal window
    # Maintain a known-good configuration
    cp .env .env.known-good
Terminal window
# Always check before committing
git status | grep -E "(\.env|backup)" && echo "⚠️ Check .env files"
# Verify .gitignore is protecting secrets
git ls-files | grep -E "\.env" && echo "⚠️ .env files in git!"

This troubleshooting guide covers the most common issues you might encounter with the database switching system. For additional help or to report new issues, refer to the main database switching guide or check the project’s GitHub repository for the latest updates and community support.