Documentation Index Fetch the complete documentation index at: https://mintlify.com/mastra-ai/mastra/llms.txt
Use this file to discover all available pages before exploring further.
Mastra supports a wide range of storage backends for different use cases and deployment environments.
PostgreSQL
Production-ready PostgreSQL adapter with full transaction support.
Installation
Configuration
import { PgStorage } from '@mastra/pg' ;
// Connection string
const storage = new PgStorage ({
connectionString: process . env . DATABASE_URL ,
});
// Or explicit configuration
const storage = new PgStorage ({
host: 'localhost' ,
port: 5432 ,
database: 'mastra' ,
user: 'postgres' ,
password: 'password' ,
schemaName: 'public' , // Optional: custom schema
});
// With connection pool options
const storage = new PgStorage ({
connectionString: process . env . DATABASE_URL ,
max: 20 , // Max connections
idleTimeoutMillis: 30000 , // Idle timeout
});
Schema Export
import { exportSchemas } from '@mastra/pg' ;
// Export DDL for all tables
const ddl = exportSchemas ( 'mastra' );
fs . writeFileSync ( 'schema.sql' , ddl );
Features
Transaction support
Connection pooling
Schema namespacing
Full-text search
JSONB columns for metadata
Performance indexes
LibSQL (Turso)
Serverless SQLite-compatible database, perfect for edge deployments.
Installation
npm install @mastra/libsql
Configuration
import { LibSQLStorage } from '@mastra/libsql' ;
// Remote (Turso)
const storage = new LibSQLStorage ({
url: process . env . TURSO_URL ,
authToken: process . env . TURSO_TOKEN ,
});
// Local file
const storage = new LibSQLStorage ({
url: 'file:local.db' ,
});
// In-memory
const storage = new LibSQLStorage ({
url: ':memory:' ,
});
Features
Edge-compatible
Low latency
Embedded mode
Sync with Turso
SQLite compatibility
MongoDB
Flexible document database for semi-structured data.
Installation
npm install @mastra/mongodb
Configuration
import { MongoStorage } from '@mastra/mongodb' ;
// MongoDB Atlas
const storage = new MongoStorage ({
uri: process . env . MONGODB_URI ,
database: 'mastra' ,
});
// Local MongoDB
const storage = new MongoStorage ({
uri: 'mongodb://localhost:27017' ,
database: 'mastra' ,
});
// With options
const storage = new MongoStorage ({
uri: process . env . MONGODB_URI ,
database: 'mastra' ,
maxPoolSize: 10 ,
minPoolSize: 2 ,
});
Features
Flexible schema
Rich querying
Aggregation pipelines
Geospatial queries
Sharding support
Upstash
Serverless Redis-compatible storage with REST API.
Installation
npm install @mastra/upstash
Configuration
import { UpstashStorage } from '@mastra/upstash' ;
const storage = new UpstashStorage ({
url: process . env . UPSTASH_REDIS_REST_URL ,
token: process . env . UPSTASH_REDIS_REST_TOKEN ,
});
Features
Edge-compatible
HTTP-based
No connection pooling needed
Global replication
Durable storage
DynamoDB
AWS fully managed NoSQL database.
Installation
npm install @mastra/dynamodb
Configuration
import { DynamoDBStorage } from '@mastra/dynamodb' ;
const storage = new DynamoDBStorage ({
region: 'us-east-1' ,
tableName: 'mastra-data' ,
// Uses AWS credentials from environment
});
Features
Serverless scaling
Single-digit millisecond latency
Multi-region replication
Point-in-time recovery
On-demand pricing
Cloudflare D1
Serverless SQL database for Cloudflare Workers.
Installation
npm install @mastra/cloudflare-d1
Configuration
import { D1Storage } from '@mastra/cloudflare-d1' ;
// In Cloudflare Worker
export default {
async fetch ( request , env ) {
const storage = new D1Storage ({
database: env . DB , // D1 database binding
});
const mastra = new Mastra ({ storage });
// ...
} ,
} ;
Features
Cloudflare Workers native
Edge deployment
SQLite-compatible
Read replication
Zero configuration
Convex
Real-time backend with built-in reactivity.
Installation
npm install @mastra/convex
Configuration
import { ConvexStorage } from '@mastra/convex' ;
const storage = new ConvexStorage ({
url: process . env . CONVEX_URL ,
});
Features
Real-time subscriptions
Reactive queries
Built-in auth
File storage
Server functions
Choosing a Storage Backend
Best for: Production applications, complex queries, ACID transactionsUse when: You need relational data, strong consistency, and advanced SQL features
Best for: Edge deployments, low latency, embedded applicationsUse when: You’re deploying to the edge or need SQLite compatibility
Best for: Flexible schemas, document-oriented data, rapid iterationUse when: Your data model is evolving or you need rich document querying
Best for: Serverless, edge, caching, session storageUse when: You need HTTP-based storage without connection management
Best for: AWS deployments, high throughput, key-value accessUse when: You’re on AWS and need serverless scaling
Best for: Cloudflare Workers, edge computeUse when: You’re building on Cloudflare’s platform
Multi-Region Setup
Deploy storage across regions:
PostgreSQL with Read Replicas
const storage = new PgStorage ({
primary: process . env . DATABASE_URL ,
replicas: [
process . env . READ_REPLICA_US ,
process . env . READ_REPLICA_EU ,
],
// Reads go to replicas, writes to primary
});
MongoDB with Sharding
const storage = new MongoStorage ({
uri: process . env . MONGODB_URI ,
database: 'mastra' ,
readPreference: 'primaryPreferred' ,
});
Storage Migration
Migrate data between backends:
import { PgStorage } from '@mastra/pg' ;
import { MongoStorage } from '@mastra/mongodb' ;
const source = new PgStorage ({ /* ... */ });
const target = new MongoStorage ({ /* ... */ });
// Migrate memory threads
const threads = await source . memory . getThreads ();
for ( const thread of threads ) {
const messages = await source . memory . getMessages ({
threadId: thread . id
});
await target . memory . saveMessages ( thread . id , messages );
}
console . log ( `Migrated ${ threads . length } threads` );
Connection Pooling
const storage = new PgStorage ({
connectionString: process . env . DATABASE_URL ,
max: 20 , // Max pool size
min: 5 , // Min pool size
idleTimeoutMillis: 30000 , // Idle timeout
connectionTimeoutMillis: 2000 , // Connection timeout
});
Indexing Strategy
Create indexes on frequently queried fields:
-- PostgreSQL example
CREATE INDEX idx_messages_thread_id ON messages(thread_id);
CREATE INDEX idx_messages_created_at ON messages(created_at);
CREATE INDEX idx_workflows_status ON workflow_runs( status );
CREATE INDEX idx_workflows_created_at ON workflow_runs(created_at);
Query Optimization
// Limit results for pagination
const messages = await storage . memory . getMessages ({
threadId ,
limit: 50 ,
offset: 0 ,
});
// Filter by date range
const recentRuns = await storage . workflows . getRuns ({
workflowId ,
startTime: new Date ( '2024-01-01' ),
endTime: new Date ( '2024-12-31' ),
});
Next Steps
Storage Overview Learn about storage architecture
Vector Stores Explore vector storage options