kratos migrate command provides tools for managing database schema migrations. Use these commands when installing Kratos for the first time or upgrading to a new version.
Always create a database backup before running migrations!Migrations modify your database schema and can’t be automatically reversed in all cases.
Commands
migrate sql
Create SQL schemas and apply migration plans.Description
This command should be run:- On a fresh SQL installation (initial setup)
- When upgrading Ory Kratos to a new minor version
- Before starting Kratos with a new database
It’s recommended to run this command close to the SQL instance (e.g., same subnet) instead of over the public internet to decrease failure risk and time required.
Supported databases
Kratos supports the following SQL databases:- PostgreSQL 11+
- MySQL 5.7+
- SQLite (not recommended for production)
- CockroachDB 20.1+
Flags
Read database connection string from environment variable
DSN or config file key dsn.Short flag: -eAccept all confirmation prompts without user interaction.Short flag:
-yPath to configuration file.
Database URL format
The database URL follows this format:Examples
Output example
migrate sql up
Apply pending migrations to bring the database schema up to date.Description
This command applies all pending migrations to upgrade your database schema to the latest version.Examples
migrate sql down
Rollback the most recent migration.Use with extreme caution!Rolling back migrations can result in data loss. Always backup your database first.
Description
This command rolls back the most recently applied migration. Use this if you need to downgrade Kratos or fix a migration issue.Flags
Number of migrations to roll back.
Read database URL from environment variable
DSN.Short flag: -eSkip confirmation prompts.Short flag:
-yExamples
migrate sql status
Show the current migration status.Description
This command displays information about applied and pending migrations.Examples
Output example
Workflows
Initial setup
When setting up Kratos for the first time:Upgrading Kratos
When upgrading to a new version:CI/CD integration
Integrate migrations into your deployment pipeline:Troubleshooting
Migration failed midway
If a migration fails partway through:- Check the error message for details
- Check migration status:
kratos migrate sql status <database-url> - Fix the underlying issue (e.g., permissions, disk space)
- Re-run the migration:
kratos migrate sql up <database-url> --yes
Locked database
If you see “database is locked” errors:Permission denied
Connection refused
- Database is running
- Connection string is correct
- Network/firewall allows connection
- Hostname and port are correct
Best practices
Always backup
Create a backup before every migration, especially in production.
Test first
Test migrations in a staging environment before running in production.
Check status
Use
migrate sql status to review pending migrations before applying them.Automate safely
Automate migrations in CI/CD, but always include proper error handling and rollback procedures.
See also
- Database configuration - Configure database connections
- Deployment guide - Production deployment best practices
- Backup and restore - Database backup strategies
- Upgrading guide - Version upgrade procedures