This guide explains how to set up the database for the CRM RAG application using the provided SQL files.
The application uses a multi-tenant architecture with two schemas:
- Public Schema (
public_schema.sql) - Contains platform-wide tables and enums - Client/Tenant Schema (
client_schema.sql) - Contains tenant-specific tables
- PostgreSQL 12 or higher
uuid-osspextensionpgvectorextension (for vector similarity search)
-- Connect to PostgreSQL as superuser
CREATE DATABASE crm_rag_db;
\c crm_rag_db;-- Enable required extensions
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgvector";# Run the public schema script
psql -d crm_rag_db -f public_schema.sqlThis will create:
- Enum types for user roles, organization status, RAG types, etc.
- Platform-wide tables (organizations, reserved_subdomains, otp)
- Indexes and triggers
- Initial data (reserved subdomains)
For each tenant organization, create a separate schema:
-- Example: Create schema for tenant "acme_corp"
CREATE SCHEMA IF NOT EXISTS acme_corp;
-- Set search path to the tenant schema
SET search_path TO acme_corp;
-- Run the client schema script (modify the file to use the correct schema name)
-- Or manually execute the CREATE TABLE statements with the correct schema# Modify client_schema.sql to use the correct schema name
# Replace 'tenant_schema_name' with 'acme_corp' in the file
# Then run the client schema script
psql -d crm_rag_db -f client_schema.sql| Table | Purpose |
|---|---|
organizations |
Platform-wide organization registry |
reserved_subdomains |
Reserved subdomain names |
otp |
One-time passwords for authentication |
| Table | Purpose |
|---|---|
users |
Tenant users with roles |
categories |
Document categories |
user_categories |
User-category associations |
knowledge_base |
Uploaded documents |
vector_doc |
Vectorized document chunks for RAG |
chat_tabs |
Chat conversation tabs |
chat_history |
Chat conversation history |
chat_tab_history_association |
Chat tab-history associations |
audit_logs |
Audit trail |
organizations (public)
↓ (1:1)
tenant_schema.users
↓ (1:many)
tenant_schema.knowledge_base
↓ (1:many)
tenant_schema.vector_doc
tenant_schema.users
↓ (many:many)
tenant_schema.categories
(via user_categories)
tenant_schema.users
↓ (1:many)
tenant_schema.chat_tabs
↓ (many:many)
tenant_schema.chat_history
(via chat_tab_history_association)
Ensure these environment variables are set in your .env file:
DATABASE_URL=postgresql://username:password@localhost:5432/crm_rag_dbThe application automatically:
- Creates public schema tables on startup
- Creates tenant-specific tables when organizations are onboarded
- Manages schema switching based on tenant context
\c crm_rag_db
\dt public.*Should show:
- organizations
- reserved_subdomains
- otp
SELECT typname FROM pg_type WHERE typnamespace = 'public'::regnamespace;Should show:
- userrole
- orgstatus
- ragtype
- kbstatus
- auditeventtype
-- Create a test tenant schema
CREATE SCHEMA test_tenant;
SET search_path TO test_tenant;
-- Run client schema creation (modify the SQL file first)
-- Then verify tables were created
\dt test_tenant.*-
Extension not found
-- Install pgvector extension CREATE EXTENSION IF NOT EXISTS "pgvector";
-
Permission denied
-- Grant necessary permissions GRANT ALL PRIVILEGES ON DATABASE crm_rag_db TO your_user; GRANT ALL PRIVILEGES ON SCHEMA public TO your_user;
-
Schema not found
-- Ensure schema exists CREATE SCHEMA IF NOT EXISTS tenant_name;
-- Check if all required extensions are installed
SELECT extname FROM pg_extension WHERE extname IN ('uuid-ossp', 'pgvector');
-- Check if all enum types exist
SELECT typname FROM pg_type WHERE typnamespace = 'public'::regnamespace;
-- Check if public schema tables exist
SELECT tablename FROM pg_tables WHERE schemaname = 'public';
-- Check if tenant schema tables exist
SELECT tablename FROM pg_tables WHERE schemaname = 'your_tenant_schema';- Vector Search: The
vector_doc.embeddingcolumn uses pgvector with IVFFlat index for efficient similarity search - Indexes: All foreign keys and commonly queried columns are indexed
- Partitioning: Consider partitioning large tables (like
vector_doc) by date if you expect high volume
# Full database backup
pg_dump -h localhost -U username -d crm_rag_db > backup.sql
# Schema-specific backup
pg_dump -h localhost -U username -d crm_rag_db --schema=public > public_backup.sql
pg_dump -h localhost -U username -d crm_rag_db --schema=tenant_name > tenant_backup.sql# Restore full database
psql -h localhost -U username -d crm_rag_db < backup.sql
# Restore specific schema
psql -h localhost -U username -d crm_rag_db < public_backup.sql
psql -h localhost -U username -d crm_rag_db < tenant_backup.sql- Row Level Security: Consider implementing RLS for multi-tenant data isolation
- Connection Pooling: Use connection pooling (e.g., PgBouncer) for production
- Encryption: Enable SSL connections and consider encrypting sensitive data
- Audit: All user actions are logged in the
audit_logstable
If you encounter issues:
- Check the application logs for detailed error messages
- Verify database permissions and extensions
- Ensure all required environment variables are set
- Check that the database URL format is correct