Skip to main content

kysera db

Database management utilities.

Commands

CommandDescription
seedRun database seeders
resetDrop all tables and re-run migrations
tablesList database tables
dumpExport database schema and data
restoreRestore database from dump
introspectAnalyze database schema
consoleInteractive database console

seed

Run database seeders.

kysera db seed

Options:

-f, --file <path> Specific seed file to run
-d, --directory <path> Seed directory (default: ./seeds)
--fresh Truncate tables before seeding
--dry-run Show what would execute
--transaction Run in single transaction
-v, --verbose Detailed output
--json Output results as JSON
-c, --config <path> Path to configuration file
-s, --schema <name> PostgreSQL schema name (default: public)

Examples:

# Run all seeds
kysera db seed

# Run specific seed file
kysera db seed --file seeds/users.ts

# Fresh seed (truncate first)
kysera db seed --fresh

# Preview
kysera db seed --dry-run

reset

Reset the database: drops every table (CASCADE), then re-runs all migrations.

kysera db reset

:::danger Destructive This is not a truncate — all tables and their data are dropped, and the schema is rebuilt from migrations. There is no way to exclude tables. Take a backup first (kysera db dump -o backup.sql). :::

Options:

--force Skip confirmation prompt
--seed Run seeds after reset
-c, --config <path> Path to configuration file
-v, --verbose Show detailed output
-s, --schema <name> PostgreSQL schema name (default: public)

tables

List database tables.

kysera db tables

Options:

--json Output as JSON
-v, --verbose Show column details
-c, --config <path> Path to configuration file
-s, --schema <name> PostgreSQL schema name (default: public)

Output:

Database Tables
===============

users (4 columns)
id serial PRIMARY KEY
email varchar(255) NOT NULL UNIQUE
name varchar(100) NOT NULL
created_at timestamp NOT NULL

posts (5 columns)
id serial PRIMARY KEY
user_id integer REFERENCES users(id)
title varchar(255) NOT NULL
content text
created_at timestamp NOT NULL

Total: 2 tables

dump

Export database schema and data.

kysera db dump

Options:

-o, --output <file> Output file path
-t, --tables <list> Comma-separated table names
--data-only Export data only (no schema)
--schema-only Export schema only (no data)
-f, --format <type> Format: sql, json (default: sql)
--json Output dump summary as JSON
-c, --config <path> Path to configuration file
-s, --schema <name> PostgreSQL schema name (default: public)

Examples:

# Full dump
kysera db dump -o backup.sql

# Schema only
kysera db dump --schema-only -o schema.sql

# Data only as JSON
kysera db dump --data-only --format json -o data.json

restore

Restore database from dump.

kysera db restore <file>

Options:

--force Skip confirmation prompt
-c, --config <path> Path to configuration file

Examples:

# Restore from SQL dump
kysera db restore backup.sql

# Restore from JSON dump
kysera db restore data.json

# Restore without confirmation
kysera db restore backup.sql --force

introspect

Introspect database schema and generate TypeScript types.

kysera db introspect [table]

Options:

--json Output as JSON
--detailed Show detailed information with TypeScript types
-c, --config <path> Path to configuration file
-s, --schema <name> PostgreSQL schema name (default: public)

Examples:

# List all tables with summary
kysera db introspect

# Introspect specific table
kysera db introspect users

# Show detailed information with TypeScript types
kysera db introspect users --detailed

# Output as JSON
kysera db introspect --json

# Introspect in specific schema
kysera db introspect --schema tenant_acme

console

Interactive database console (REPL).

kysera db console

Options:

-e, --execute <sql> Execute SQL query and exit
--force Skip confirmation for destructive queries
-c, --config <path> Path to configuration file

Destructive statements (DROP, DELETE, TRUNCATE, ...) prompt for confirmation before running — --force skips the prompt, which is what you need when piping a statement through -e in a script.

Console Commands:

.help, .h Show help
.exit, .quit, .q Exit the console
.tables, .t List all tables
.describe, .d <table> Describe table structure
.indexes, .i <table> Show table indexes
.count, .c <table> Count rows in table
.clear, .cls Clear the screen

Examples:

# Open interactive console
kysera db console

# Execute single query
kysera db console -e "SELECT * FROM users LIMIT 5"

Seed File Structure

// seeds/users.ts
import { Kysely } from 'kysely'

export async function seed(db: Kysely<any>): Promise<void> {
await db
.insertInto('users')
.values([
{ email: 'admin@example.com', name: 'Admin' },
{ email: 'user@example.com', name: 'User' }
])
.execute()
}

Best Practices

1. Use Transactions for Seeds

kysera db seed --transaction

2. Order Seed Files

seeds/
├── 01_users.ts
├── 02_posts.ts
└── 03_comments.ts

3. Backup Before Reset

kysera db dump -o backup.sql
kysera db reset --force