Skip to main content

Database Setup

Medusa uses PostgreSQL as its primary database. This guide covers database setup, configuration, and migrations.

PostgreSQL Requirements

  • PostgreSQL version: 12 or higher (PostgreSQL 14+ recommended)
  • Database user: Must have create privileges
  • Extensions: No special extensions required by default

Database Connection

Connection URL

Medusa connects to PostgreSQL using a connection URL:
Where:
  • [user]: Your PostgreSQL username (required)
  • [:password]: User password (optional, prefix with :)
  • [host]: Database host (required, e.g., localhost)
  • [:port]: PostgreSQL port (optional, default: 5432, prefix with :)
  • [dbname]: Database name (required)

Example Connection URLs

Creating a Database

Using PostgreSQL CLI

Using createdb Command

Medusa can automatically create the database on first run if it doesn’t exist and the user has create privileges.

Database Configuration

Basic Configuration

medusa-config.ts

Advanced Configuration

medusa-config.ts

Connection Pool Sizing

Recommended pool sizes based on your deployment:
  • Development: min: 2, max: 5
  • Production (single instance): min: 2, max: 10
  • Production (multiple instances): min: 2, max: 5 per instance
Be careful with connection pool sizes. PostgreSQL has a maximum connection limit (default: 100). If you have multiple application instances, ensure the total connections don’t exceed the limit.

SSL Configuration

For production databases, enable SSL:
SSL modes:
  • disable: No SSL
  • require: SSL required, but don’t verify certificate
  • verify-ca: Verify server certificate against CA
  • verify-full: Verify certificate and hostname

Database Migrations

Migrations ensure your database schema is up-to-date with your Medusa version and modules.

Running Migrations

Before starting your application, run migrations:
This command:
  1. Creates the database if it doesn’t exist
  2. Creates the migrations table
  3. Runs all pending module migrations
  4. Synchronizes link definitions between modules
  5. Executes migration scripts

Migration Options

Always run migrations before starting your application in production. Running without migrations will cause errors.

Migration Process

Medusa uses MikroORM for migrations. The migration process:
  1. Module Migrations: Each module maintains its own migrations
  2. Link Synchronization: Creates join tables for module relationships
  3. Migration Scripts: Custom data transformations and updates

Checking Migration Status

Migrations are tracked in the mikro_orm_migrations table:

Database Schemas

By default, Medusa uses the public schema. You can configure a custom schema:
medusa-config.ts
Create the schema before running migrations:

Database Backup and Restore

Backup Database

Restore Database

Always test your backup and restore process before you need it in production.

Performance Optimization

Indexes

Medusa automatically creates necessary indexes through migrations. For custom optimizations:

Connection Pooling

Use connection pooling for better performance:
medusa-config.ts

Query Performance

Monitor slow queries:

Troubleshooting

Connection Refused

Solutions:
  • Check if PostgreSQL is running: sudo systemctl status postgresql
  • Verify the host and port in DATABASE_URL
  • Check firewall rules

Authentication Failed

Solutions:
  • Verify username and password in DATABASE_URL
  • Check pg_hba.conf for authentication settings
  • Reset user password if needed

Database Does Not Exist

Solutions:
  • Create the database manually
  • Ensure the user has create privileges for auto-creation
  • Check the database name in DATABASE_URL

Too Many Connections

Solutions:
  • Reduce connection pool size in databaseDriverOptions
  • Increase PostgreSQL max_connections setting
  • Use a connection pooler like PgBouncer

Migration Failures

Solutions:
  • Check PostgreSQL logs for details
  • Ensure user has necessary privileges
  • Verify no concurrent migration processes
  • Check for conflicting data or constraints

Production Best Practices

Security

  1. Use strong passwords: Generate random, complex passwords
  2. Limit privileges: Grant only necessary permissions
  3. Enable SSL: Use SSL for all connections
  4. Network isolation: Use private networks or VPNs
  5. Regular backups: Automate daily backups

Monitoring

  1. Connection count: Monitor active connections
  2. Query performance: Track slow queries
  3. Disk usage: Monitor database size
  4. Replication lag: If using replication

Maintenance

Schedule regular VACUUM and ANALYZE operations during low-traffic periods to maintain optimal performance.

Multiple Database Support

Medusa supports module-specific databases:
medusa-config.ts
Environment variables: