Complete Supabase setup for Mem0 OSS including PostgreSQL schema with pgvector for embeddings, memory_relationships tables for graph memory, RLS policies for user/tenant isolation, performance indexes, connection pooling, and backup/migration strategies. Use when setting up Mem0 with Supabase, configuring OSS memory backend, implementing memory persistence, migrating from Platform to OSS, or when user mentions Mem0 Supabase, memory database, pgvector for Mem0, memory isolation, or Mem0 backup.
Limited to specific tools
Additional assets for this skill
This skill is limited to using the following tools:
README.mdexamples/multi-tenant-pattern.mdexamples/platform-to-oss-migration-guide.mdexamples/user-isolation-pattern.mdscripts/apply-mem0-rls.shscripts/apply-mem0-schema.shscripts/create-mem0-indexes.shscripts/setup-mem0-pgvector.shscripts/validate-mem0-setup.shscripts/verify-supabase-setup.shtemplates/mem0-basic-config.pytemplates/mem0-enterprise-config.pytemplates/mem0-graph-config.pytemplates/mem0-schema-graph.sqltemplates/mem0-schema.sqlComplete guide for setting up Supabase as the backend for Mem0 Open Source (self-hosted) mode, including PostgreSQL schema with pgvector, RLS policies for security, and production-ready configurations.
Prerequisites Check:
Verify Supabase is initialized:
bash scripts/verify-supabase-setup.sh
If not initialized, set up Supabase first:
/supabase:init commandEnvironment Configuration:
# Required environment variables
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_KEY=your-service-key
SUPABASE_DB_URL=postgresql://postgres:[password]@db.[project].supabase.co:5432/postgres
# Optional: Mem0-specific configs
MEM0_EMBEDDING_MODEL=text-embedding-3-small
MEM0_VECTOR_DIMENSION=1536
Enable Extension:
bash scripts/setup-mem0-pgvector.sh
This script:
Manual Verification:
-- Check pgvector is enabled
SELECT * FROM pg_extension WHERE extname = 'vector';
-- Test vector operations
SELECT '[1,2,3]'::vector;
Apply Memory Schema:
bash scripts/apply-mem0-schema.sh
This creates three core tables:
1. memories table (vector storage):
id (uuid, primary key)user_id (text, indexed) - User identifier for isolationagent_id (text, indexed, nullable) - Agent identifierrun_id (text, indexed, nullable) - Session/conversation identifiermemory (text) - Memory contenthash (text) - Content hash for deduplicationmetadata (jsonb) - Flexible metadata storagecategories (text[]) - Memory categorizationembedding (vector(1536)) - Semantic embeddingcreated_at (timestamptz)updated_at (timestamptz)2. memory_relationships table (graph memory):
id (uuid, primary key)source_memory_id (uuid, foreign key)target_memory_id (uuid, foreign key)relationship_type (text) - e.g., "references", "caused_by", "related_to"strength (numeric) - Relationship strength (0.0-1.0)metadata (jsonb)user_id (text, indexed) - For RLS isolationcreated_at (timestamptz)3. memory_history table (audit trail):
id (uuid, primary key)memory_id (uuid)operation (text) - "create", "update", "delete"old_value (jsonb)new_value (jsonb)user_id (text)timestamp (timestamptz)Use Template for Custom Schema:
# Generate schema with custom dimensions
bash scripts/generate-mem0-schema.sh \
--dimensions 1536 \
--include-graph true \
--include-history true \
> custom-mem0-schema.sql
# Apply custom schema
psql $SUPABASE_DB_URL < custom-mem0-schema.sql
Apply Optimized Indexes:
bash scripts/create-mem0-indexes.sh
Index Strategy:
Vector Search Indexes (HNSW):
-- Main embedding index (cosine distance)
CREATE INDEX idx_memories_embedding ON memories
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
User/Agent Isolation Indexes:
CREATE INDEX idx_memories_user_id ON memories(user_id);
CREATE INDEX idx_memories_agent_id ON memories(agent_id);
CREATE INDEX idx_memories_run_id ON memories(run_id);
Composite Indexes for Common Queries:
-- User + timestamp for chronological retrieval
CREATE INDEX idx_memories_user_created ON memories(user_id, created_at DESC);
-- User + agent for agent-specific memories
CREATE INDEX idx_memories_user_agent ON memories(user_id, agent_id);
Graph Relationship Indexes:
CREATE INDEX idx_relationships_source ON memory_relationships(source_memory_id);
CREATE INDEX idx_relationships_target ON memory_relationships(target_memory_id);
CREATE INDEX idx_relationships_type ON memory_relationships(relationship_type);
Full-Text Search Index (optional):
CREATE INDEX idx_memories_content_fts ON memories
USING gin(to_tsvector('english', memory));
Index Selection Guide:
Apply RLS Policies:
bash scripts/apply-mem0-rls.sh
Security Patterns:
1. User Isolation (Default):
-- Users can only access their own memories
CREATE POLICY "Users access own memories"
ON memories FOR ALL
USING (auth.uid()::text = user_id);
-- Users can only see their own relationships
CREATE POLICY "Users access own relationships"
ON memory_relationships FOR ALL
USING (auth.uid()::text = user_id);
2. Multi-Tenant Isolation (Enterprise):
-- Check organization membership
CREATE POLICY "Organization members access memories"
ON memories FOR ALL
USING (
EXISTS (
SELECT 1 FROM org_members
WHERE org_members.user_id = auth.uid()::text
AND org_members.org_id = memories.metadata->>'org_id'
)
);
3. Agent-Specific Policies:
-- Public agent memories (shared across users)
CREATE POLICY "Agent memories readable by all"
ON memories FOR SELECT
USING (agent_id IS NOT NULL AND user_id IS NULL);
-- Agent can write to their own memory space
CREATE POLICY "Agent writes own memories"
ON memories FOR INSERT
WITH CHECK (agent_id = current_setting('app.agent_id', true));
Test RLS Enforcement:
bash scripts/test-mem0-rls.sh --user-id "test-user-123"
Setup PgBouncer (Recommended for Production):
bash scripts/configure-connection-pool.sh
Connection Pooling Strategy:
For Mem0 OSS:
Configuration:
# Mem0 with connection pooling
from mem0 import Memory
config = {
"vector_store": {
"provider": "postgres"
"config": {
"url": "postgresql://user:pass@pooler.project.supabase.co:6543/postgres"
"pool_size": 20
"max_overflow": 10
"pool_timeout": 30
"pool_recycle": 3600
}
}
}
memory = Memory.from_config(config)
Supabase Pooler URLs:
pooler.project.supabase.co:6543pooler.project.supabase.co:5432Setup Automated Backups:
bash scripts/setup-mem0-backup.sh --schedule daily --retention 30
Backup Strategies:
1. Point-in-Time Recovery (Supabase Built-in):
2. Manual SQL Dumps:
# Full database backup
bash scripts/backup-mem0-memories.sh
# Incremental backup (changes since last backup)
bash scripts/backup-mem0-memories.sh --incremental --since "2025-10-01"
# Backup to S3
bash scripts/backup-mem0-memories.sh --destination s3://my-bucket/mem0-backups/
3. Selective Backups:
# Backup specific user's memories
bash scripts/backup-user-memories.sh --user-id "customer-123"
# Backup by date range
bash scripts/backup-mem0-memories.sh --from "2025-01-01" --to "2025-10-27"
Restore Procedures:
# Restore from backup file
bash scripts/restore-mem0-backup.sh backup-2025-10-27.sql
# Restore specific user
bash scripts/restore-user-memories.sh backup-user-123.sql --user-id "customer-123"
Export from Mem0 Platform:
bash scripts/export-from-platform.sh \
--api-key "your-platform-api-key" \
--output platform-export.json
Transform and Import to Supabase:
bash scripts/migrate-platform-to-oss.sh \
--input platform-export.json \
--supabase-url $SUPABASE_URL \
--dry-run # Test first
# After validation, run actual migration
bash scripts/migrate-platform-to-oss.sh \
--input platform-export.json \
--supabase-url $SUPABASE_URL
Migration Steps:
Rollback Plan:
# Create migration checkpoint before starting
bash scripts/create-migration-checkpoint.sh
# Rollback if issues occur
bash scripts/rollback-migration.sh --checkpoint checkpoint-2025-10-27
Run Complete Validation Suite:
bash scripts/validate-mem0-setup.sh
Validation Checks:
Performance Benchmarks:
bash scripts/benchmark-mem0-performance.sh
Expected Performance (1K-10K memories):
Use template: templates/mem0-basic-config.py
from mem0 import Memory
config = {
"vector_store": {
"provider": "postgres"
"config": {
"url": "postgresql://postgres:[password]@db.[project].supabase.co:5432/postgres"
"table_name": "memories"
"embedding_dimension": 1536
}
}
}
memory = Memory.from_config(config)
Use template: templates/mem0-graph-config.py
from mem0 import Memory
config = {
"vector_store": {
"provider": "postgres"
"config": {
"url": os.getenv("SUPABASE_DB_URL")
"table_name": "memories"
"embedding_dimension": 1536
}
}
"graph_store": {
"provider": "postgres", # Using same DB for graph
"config": {
"url": os.getenv("SUPABASE_DB_URL")
"relationship_table": "memory_relationships"
}
}
"version": "v1.1"
}
memory = Memory.from_config(config)
Use template: templates/mem0-enterprise-config.py
Includes:
Scenario: SaaS app with user-specific memories
Implementation: See examples/user-isolation-pattern.md
Key Points:
Scenario: Teams/organizations share memories within org
Implementation: See examples/multi-tenant-pattern.md
Key Points:
Scenario: Shared agent memories across all users
Implementation: See examples/agent-knowledge-pattern.md
Key Points:
Scenario: Temporary conversation context
Implementation: See examples/session-memory-pattern.md
Key Points:
Problem: Extension not available
ERROR: extension "vector" is not available
Solution:
SELECT * FROM pg_extension WHERE extname = 'vector';Problem: Queries taking > 500ms
Solutions:
Check index exists:
SELECT indexname FROM pg_indexes
WHERE tablename = 'memories' AND indexname LIKE '%embedding%';
Reduce search space with filters:
memory.search(query, user_id="specific-user", limit=5)
Increase HNSW parameters (rebuild index):
DROP INDEX idx_memories_embedding;
CREATE INDEX idx_memories_embedding ON memories
USING hnsw (embedding vector_cosine_ops)
WITH (m = 32, ef_construction = 128);
Switch to IVFFlat for large datasets (> 1M):
bash scripts/migrate-to-ivfflat.sh
Problem: Queries return empty results despite data existing
Solution:
Check RLS is enabled:
SELECT tablename, rowsecurity FROM pg_tables
WHERE tablename = 'memories';
Verify auth context is set:
# Ensure user_id in JWT
supabase.auth.get_user()
Test with service key (bypasses RLS):
bash scripts/test-mem0-rls.sh --bypass-rls
Review policy logs:
bash scripts/debug-rls-policies.sh
Problem: "too many connections" errors
Solutions:
bash scripts/monitor-connections.sh
Problem: Migration script fails partway through
Recovery:
# Check migration status
bash scripts/check-migration-status.sh
# Rollback to checkpoint
bash scripts/rollback-migration.sh --checkpoint last
# Resume from last successful batch
bash scripts/resume-migration.sh
Run regular security audits:
bash scripts/audit-mem0-security.sh --report security-audit.md
Checks:
Scripts (all executable, production-ready):
scripts/verify-supabase-setup.sh - Check Supabase initializationscripts/setup-mem0-pgvector.sh - Enable pgvector extensionscripts/apply-mem0-schema.sh - Create memory tablesscripts/generate-mem0-schema.sh - Generate custom schemascripts/create-mem0-indexes.sh - Create optimized indexesscripts/apply-mem0-rls.sh - Apply RLS policiesscripts/test-mem0-rls.sh - Test security policiesscripts/configure-connection-pool.sh - Setup poolingscripts/setup-mem0-backup.sh - Configure backupsscripts/backup-mem0-memories.sh - Manual backupscripts/restore-mem0-backup.sh - Restore from backupscripts/backup-user-memories.sh - User-specific backupscripts/export-from-platform.sh - Export Platform memoriesscripts/migrate-platform-to-oss.sh - Platform → OSS migrationscripts/validate-mem0-setup.sh - Complete validationscripts/benchmark-mem0-performance.sh - Performance testingscripts/migrate-to-ivfflat.sh - Switch to IVFFlat indexscripts/debug-rls-policies.sh - Debug RLS issuesscripts/monitor-connections.sh - Connection monitoringscripts/audit-mem0-security.sh - Security auditTemplates:
templates/mem0-schema.sql - Base schema with pgvectortemplates/mem0-schema-graph.sql - Schema with graph supporttemplates/mem0-indexes.sql - Performance indexestemplates/mem0-rls-policies.sql - Security policiestemplates/mem0-basic-config.py - Basic Python configtemplates/mem0-graph-config.py - Full-featured configtemplates/mem0-enterprise-config.py - Multi-tenant configtemplates/backup-policy.yaml - Backup configurationtemplates/connection-pool-config.ini - PgBouncer configExamples:
examples/user-isolation-pattern.md - User-specific memoriesexamples/multi-tenant-pattern.md - Organization isolationexamples/agent-knowledge-pattern.md - Shared agent memoriesexamples/session-memory-pattern.md - Temporary session contextexamples/platform-to-oss-migration-guide.md - Complete migration walkthroughexamples/backup-restore-procedures.md - Disaster recovery guideexamples/performance-tuning-guide.md - Optimization strategiesPlugin: mem0 Version: 1.0.0 Last Updated: 2025-10-27