Migrate from Supabase to direct PostgreSQL connection

- Replace @supabase/supabase-js with native pg library
- Rewrite database.js to use PostgreSQL connection pool
- Update server.js with PostgreSQL connection testing
- Create postgres-schema.sql with complete database schema
- Add apply-schema.js script for easy schema deployment
- Update all documentation (README.md, DEPLOYMENT.md, deploy.sh)
- Remove Supabase-specific files and references
- Update db.config.example.js with PostgreSQL format
This commit is contained in:
2025-12-22 12:54:36 +11:00
parent 90cf68327a
commit 0a8ea2b603
234 changed files with 754 additions and 33727 deletions

View File

@@ -1,45 +1,46 @@
# Database Configuration Setup - Supabase
# Database Configuration Setup - PostgreSQL
## Overview
Database credentials are stored in a separate configuration file (`db.config.js`) that is **NOT committed to GitHub** for security reasons.
This project now uses **Supabase** (PostgreSQL) instead of MySQL.
This project uses **PostgreSQL** for persistent data storage.
## Files
### 1. `db.config.example.js` (Committed to Git)
Template file showing the required Supabase configuration structure.
Template file showing the required PostgreSQL configuration structure.
### 2. `db.config.js` (NOT Committed - in .gitignore)
Contains actual Supabase credentials. This file must be created manually.
Contains actual PostgreSQL credentials. This file must be created manually.
### 3. `.gitignore`
Ensures `db.config.js` is never committed to the repository.
## Quick Setup
See **[SUPABASE_SETUP.md](SUPABASE_SETUP.md)** for detailed step-by-step instructions.
### Quick Start
1. **Create Supabase project** at [app.supabase.com](https://app.supabase.com)
2. **Copy credentials** from Project Settings → API
3. **Update `db.config.js`:**
1. **Ensure PostgreSQL is running** on your server
2. **Create the database:** `CREATE DATABASE connect5;`
3. **Run the schema:** `psql -h HOST -U postgres -d connect5 -f postgres-schema.sql`
4. **Create `db.config.js`:**
```javascript
module.exports = {
supabaseUrl: 'https://xxxxx.supabase.co',
supabaseAnonKey: 'eyJhbGci...',
supabasePassword: 't1hWsackxbYzRIPD'
HOST: '202.171.184.108',
USER: 'postgres',
PASSWORD: 'your-password',
DB: 'connect5',
dialect: 'postgres',
pool: { max: 5, min: 0, acquire: 30000, idle: 10000 }
};
```
4. **Run SQL schema** in Supabase SQL Editor (see SUPABASE_SETUP.md)
5. **Start server:** `npm start`
## Security Features
✅ **Credentials not in git** - `db.config.js` is in `.gitignore`
✅ **Template provided** - `db.config.example.js` shows the structure
✅ **Supabase RLS** - Row Level Security policies protect data
✅ **Connection pooling** - Efficient database connection management
✅ **Separate config** - Easy to update without touching main code
## Troubleshooting
@@ -49,28 +50,29 @@ See **[SUPABASE_SETUP.md](SUPABASE_SETUP.md)** for detailed step-by-step instruc
**Solution:** Create the `db.config.js` file:
```bash
cp db.config.example.js db.config.js
# Then edit with your Supabase credentials
# Then edit with your PostgreSQL credentials
```
### Error: Invalid API key
### Error: ECONNREFUSED or connection timeout
**Solution:** Check your credentials in `db.config.js`:
- Verify `supabaseUrl` is correct
- Verify `supabaseAnonKey` (should start with `eyJ...`)
- Get credentials from Supabase dashboard → Project Settings → API
- Verify `HOST` is accessible
- Verify `USER` and `PASSWORD` are correct
- Ensure PostgreSQL server is running: `sudo systemctl status postgresql`
- Check firewall allows connection to port 5432
### Error: Table 'players' does not exist
**Solution:**
- Run the SQL schema in Supabase SQL Editor
- See SUPABASE_SETUP.md Step 4 for the complete schema
- Run the SQL schema: `psql -h HOST -U postgres -d connect5 -f postgres-schema.sql`
- See README.md for setup instructions
## Important Notes
⚠️ **NEVER commit `db.config.js` to git**
⚠️ **Keep credentials secure**
⚠️ **Use different projects for dev/prod**
⚠️ **The anon key is safe for client-side use** (protected by RLS)
⚠️ **Use different databases for dev/prod**
⚠️ **Configure PostgreSQL firewall rules appropriately**
## File Structure
@@ -80,7 +82,6 @@ Connect-5/
├── db.config.js ← Your credentials (NOT in git)
├── .gitignore ← Protects db.config.js
├── database.js ← Imports from db.config.js
├── supabase-functions.sql ← Helper functions for Supabase
├── SUPABASE_SETUP.md ← Detailed setup guide
├── postgres-schema.sql ← Database schema
└── README_DB_CONFIG.md ← This file
```