Skip to main content
Ory Kratos supports multiple SQL databases for production deployments. Choose the database that best fits your infrastructure requirements.

Supported databases

PostgreSQL

Recommended for most production deployments. Supports all Kratos features.

MySQL

Alternative SQL database with full feature support.

CockroachDB

Distributed SQL database for high availability and global deployments.

SQLite

Development and testing only. Not recommended for production.

Database connection strings (DSN)

Configure the database connection using the DSN environment variable or in the configuration file.
DSN="postgres://user:password@localhost:5432/kratos?sslmode=require&max_conns=20&max_idle_conns=4"
DSN parameters:
  • sslmode - SSL mode (require, verify-ca, verify-full, disable)
  • max_conns - Maximum number of connections (default: unlimited)
  • max_idle_conns - Maximum idle connections (default: 2)
  • conn_max_lifetime - Connection maximum lifetime (e.g., 1h)

PostgreSQL setup

1

Install PostgreSQL

Install PostgreSQL 12 or later:
sudo apt update
sudo apt install postgresql postgresql-contrib
2

Create database and user

CREATE USER kratos WITH PASSWORD 'your-secure-password';
CREATE DATABASE kratos;
GRANT ALL PRIVILEGES ON DATABASE kratos TO kratos;
3

Configure Kratos

Set the DSN in your configuration:
dsn: postgres://kratos:your-secure-password@localhost:5432/kratos?sslmode=require
4

Run migrations

kratos migrate sql -e --yes

MySQL setup

1

Install MySQL

Install MySQL 8.0 or later:
sudo apt update
sudo apt install mysql-server
2

Create database and user

CREATE USER 'kratos'@'%' IDENTIFIED BY 'your-secure-password';
CREATE DATABASE kratos CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON kratos.* TO 'kratos'@'%';
FLUSH PRIVILEGES;
3

Configure Kratos

dsn: mysql://kratos:your-secure-password@tcp(localhost:3306)/kratos?parseTime=true
4

Run migrations

kratos migrate sql -e --yes

CockroachDB setup

1

Install CockroachDB

curl https://binaries.cockroachdb.com/cockroach-v23.1.0.linux-amd64.tgz | tar -xz
sudo cp cockroach-v23.1.0.linux-amd64/cockroach /usr/local/bin/
cockroach start-single-node --insecure --listen-addr=localhost:26257
2

Create database and user

CREATE USER kratos;
CREATE DATABASE kratos;
GRANT ALL ON DATABASE kratos TO kratos;
3

Configure Kratos

dsn: cockroach://kratos@localhost:26257/kratos?sslmode=disable
Use proper SSL certificates in production. Never use sslmode=disable in production.
4

Run migrations

kratos migrate sql -e --yes

Connection pool configuration

Optimize database connections for your workload:
dsn: postgres://user:pass@host/db?max_conns=20&max_idle_conns=4&conn_max_lifetime=1h
max_conns
number
Maximum number of open connections. Set based on your database server capacity and expected load.Recommended values:
  • Small deployment: 10-20
  • Medium deployment: 20-50
  • Large deployment: 50-100
max_idle_conns
number
Maximum number of idle connections. Generally 20-25% of max_conns.
conn_max_lifetime
duration
Maximum lifetime of a connection. Helps prevent stale connections. Recommended: 1h to 4h.

Database migrations

Kratos uses automatic schema migrations. See the migration guide for details.

Performance tuning

Optimize PostgreSQL for Kratos:
-- In postgresql.conf
shared_buffers = 256MB
effective_cache_size = 1GB
maintenance_work_mem = 64MB
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
random_page_cost = 1.1
effective_io_concurrency = 200
work_mem = 4MB
min_wal_size = 1GB
max_wal_size = 4GB
Optimize MySQL for Kratos:
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
max_connections = 500

Backup and recovery

Always implement regular database backups for production deployments.
# Backup
pg_dump -U kratos -h localhost kratos > kratos_backup.sql

# Restore
psql -U kratos -h localhost kratos < kratos_backup.sql

Troubleshooting

Check that:
  1. Database is running
  2. Firewall allows connections
  3. Database is listening on the correct interface
  4. Credentials are correct
For production, always use SSL:
# PostgreSQL with SSL
DSN="postgres://user:pass@host/db?sslmode=require"

# MySQL with SSL
DSN="mysql://user:pass@tcp(host)/db?tls=true"
Increase max_connections in your database configuration or reduce max_conns in the Kratos DSN.

Next steps

Run migrations

Learn about database migration management

Configuration

Complete configuration reference

Build docs developers (and LLMs) love