The application has been migrated from SQLite to PostgreSQL for better production reliability and scalability.
- Install PostgreSQL dependencies:
cd backend pip install -r requirements.txt
-
Install PostgreSQL:
- Windows: Download from https://www.postgresql.org/download/windows/
- Mac:
brew install postgresql - Linux:
sudo apt-get install postgresql postgresql-contrib
-
Create Database:
createdb sql_learning
-
Update Environment Variables: Edit
backend/.env:DATABASE_URL=postgresql://username:password@localhost:5432/sql_learning
-
Create Supabase Account:
- Go to https://supabase.com
- Create new project
- Note down your database URL
-
Get Connection String:
- Go to Settings > Database
- Copy the connection string
- Replace
[YOUR-PASSWORD]with your actual password
-
Update Environment Variables: Edit
backend/.env:DATABASE_URL=postgresql://postgres:[YOUR-PASSWORD]@db.[YOUR-PROJECT-REF].supabase.co:5432/postgres
Neon (Free Tier):
- Website: https://neon.tech
- Offers generous free tier with serverless PostgreSQL
Railway:
- Website: https://railway.app
- Simple PostgreSQL deployment
ElephantSQL:
- Website: https://www.elephantsql.com
- Managed PostgreSQL service
The application will automatically create all necessary tables on first run using the PostgreSQL schema in database_postgresql.sql.
-
Database Connection:
- Replaced SQLite with asyncpg for PostgreSQL
- Added connection pooling with SQLAlchemy
- Asynchronous database operations with sync wrappers
-
Schema Conversion:
INTEGER PRIMARY KEY AUTOINCREMENT→SERIAL PRIMARY KEYREAL→DECIMAL- Added proper foreign key constraints
- Added performance indexes
-
Query Compatibility:
- Updated parameter placeholders from
?to$1, $2, etc. - Modified INSERT queries to use
RETURNING id - Added proper error handling
- Updated parameter placeholders from
-
Start the Application:
cd backend python -m uvicorn app.main:app --reload -
Verify Database Connection:
- Check logs for "Database initialized successfully!"
- Test API endpoints at http://localhost:8000/docs
-
Test Analytics Storage:
- Try practice questions and check if progress is saved
- Visit analytics page to verify data persistence
Connection Issues:
- Verify DATABASE_URL format
- Check firewall settings
- Ensure PostgreSQL service is running
Permission Issues:
- Verify user has CREATE/INSERT/UPDATE permissions
- Check database exists and is accessible
Migration Errors:
- Check PostgreSQL version compatibility (9.6+)
- Verify all dependencies are installed
- Review logs for specific error messages
- Production Reliability: No more readonly database errors
- Scalability: Better handling of concurrent users
- Data Integrity: Proper foreign key constraints
- Performance: Optimized indexes and query planning
- Features: Full-text search, JSON operations, advanced analytics
If needed, you can temporarily rollback to SQLite by:
- Changing DATABASE_URL to SQLite format in
.env - Reverting database.py imports to original SQLite functions
- Using the original
database_simple.sqlschema
However, PostgreSQL is strongly recommended for production use.