Commonly has been updated to use PostgreSQL as the default storage for all chat messages, while maintaining MongoDB for user management and pod metadata. This document describes the current implementation and key changes.
| Component | MongoDB | PostgreSQL |
|---|---|---|
| Users | ✅ Primary (auth, profiles) | 🔄 Sync (for message joins) |
| Pods | ✅ Primary (metadata, membership) | 🔄 Sync (for chat functionality) |
| Messages | ❌ Fallback only | ✅ Primary Storage |
| Posts | ✅ Primary | ❌ Not used |
1. User sends message
↓
2. Check pod membership (MongoDB)
↓
3. Store message (PostgreSQL)
↓
4. Broadcast via Socket.io
↓
5. Retrieve messages (PostgreSQL)
messageController.js: Now uses PostgreSQL for all message operationsgetMessages(): Retrieves from PostgreSQL with MongoDB membership checkcreateMessage(): Stores in PostgreSQL after MongoDB authorizationdeleteMessage(): Deletes from PostgreSQL with MongoDB permission check
- Real-time messaging: Uses PostgreSQL for storage with MongoDB for authorization
- Fallback mechanism: Falls back to MongoDB if PostgreSQL fails
- Consistent flow: Both manual and socket messages use same PostgreSQL storage
- AgentMessageService: Posts agent messages into PostgreSQL when available
- User synchronization: Agent users are auto-synced to PostgreSQL users table
- One-time sync: Efficient checking to avoid unnecessary user syncing
Messages are stored and retrieved in chronological order (oldest first):
- PostgreSQL query:
ORDER BY created_at ASC - Frontend display: Shows messages in chronological conversation flow
PG_HOST=YOUR_PG_HOST
PG_PORT=25450
PG_USER=avnadmin
PG_PASSWORD=[REDACTED]
PG_DATABASE=defaultdb
PG_SSL_CA_PATH=/app/ca.pem- Development: Uses external PostgreSQL with file mounting for live reloading
- Production: Same PostgreSQL with optimized container builds
-- Messages (primary storage)
CREATE TABLE messages (
id SERIAL PRIMARY KEY,
pod_id VARCHAR(24) REFERENCES pods(id),
user_id VARCHAR(24) NOT NULL,
content TEXT NOT NULL,
message_type VARCHAR(20) DEFAULT 'text',
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Users (synchronized from MongoDB)
CREATE TABLE users (
_id VARCHAR(24) PRIMARY KEY,
username VARCHAR(100) NOT NULL,
profile_picture TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Pods (synchronized from MongoDB)
CREATE TABLE pods (
id VARCHAR(24) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
type VARCHAR(50) NOT NULL,
created_by VARCHAR(24) NOT NULL
);- Message Persistence: Send message → refresh page → message should persist
- Message Ordering: Messages appear in chronological order (oldest first)
- Bot Messages: Discord integration messages show the Commonly Bot agent user (
commonly-bot) instead of "Unknown User" - Real-time Updates: Messages appear immediately via Socket.io
- Fallback: System gracefully falls back to MongoDB if PostgreSQL fails
# Restart environment with changes
./dev.sh restart
# Check PostgreSQL connection
./dev.sh logs backend | grep PostgreSQL
# Test message persistence
# 1. Send message in pod
# 2. Refresh browser
# 3. Verify message persists-
"Unknown User" in messages
- Cause: User not synchronized to PostgreSQL
- Fix: User sync happens automatically on first message
-
Messages disappear after refresh
- Cause: PostgreSQL connection failed, fell back to MongoDB
- Check: Backend logs for PostgreSQL connection errors
-
Message order reversed
- Cause: PostgreSQL query ordering mismatch
- Fix: Ensure
ORDER BY created_at ASCin Message model
# Check PostgreSQL connection
✅ PostgreSQL connected successfully
✅ PostgreSQL routes registered for chat functionality
# Check message creation
✅ Discord summary message created in PostgreSQL
✅ Commonly Bot agent user synchronized to PostgreSQL: commonly-bot
# Check for errors
❌ PostgreSQL connection error: [error details]
❌ SQL Error in Message.create: [error details]- Indexing: Optimize PostgreSQL indexes for message queries
- Connection Pooling: Monitor PostgreSQL connection pool usage
- Caching: Consider Redis for frequently accessed messages
- Data Migration: Migrate existing MongoDB messages to PostgreSQL
- Cleanup: Remove MongoDB message fallback code
- Monitoring: Add PostgreSQL health checks and metrics
backend/controllers/messageController.js- Message CRUD operationsbackend/controllers/podController.js- Pod management (dual database)
backend/models/pg/Message.js- PostgreSQL message modelbackend/models/pg/Pod.js- PostgreSQL pod reference model (reference-only; MongoDBPodis authoritative)backend/models/Message.js- MongoDB message model (fallback)
backend/services/agentMessageService.js- Agent message handlingbackend/server.js- Socket.io message routing
backend/config/db-pg.js- PostgreSQL connectionbackend/config/schema.sql- PostgreSQL schema definitiondocker-compose.dev.yml- Development environment setup