2. Your first database
- Category: Tutorial
What you will learn
- What is inside a KDB database folder.
- How to use
kdb shell(type SQL) andkdb sql(run SQL once). - What the exit codes mean.
The examples use the database market with the sample data (see the quick start).
A database is a folder
A KDB database is one folder. You choose the name. When you open a folder that does not exist, KDB creates a new empty database there.
Get-ChildItem market -Name
files
wal
wal_archive
_trash
kdb.lock
_manifest.a
_manifest.b
| Name | What it is |
|---|---|
files\ | The data files. One file for each table part (partition), plus the table list (catalog). |
wal\ | The write-ahead log. Every change goes here first. |
wal_archive\ | Old log files. Only backups and repairs use them. The oldest are deleted after 1 GB (changeable: ALTER DATABASE ... SET WAL_ARCHIVE, or wal_archive_mb in kdb.conf for all databases). |
_trash\ | Old file versions. KDB deletes them after 1 hour (trash_keep_hours in kdb.conf, or KDB_TRASH_KEEP_HOURS). |
kdb.lock | Only one program may open the database. This file is locked while it is open. |
_manifest.a, _manifest.b | The list of current files. It is saved twice, for safety. |
Rules:
- Do not change or delete files in the folder by hand. Use
kdbcommands. - To copy a database, stop every program that uses it first. Better: use
kdb backup(chapter 9). - Only one program can open a database at a time. While
kdb serveruns,kdb sqlon the same folder fails with "the database is open in another program". Use the server's HTTP API then (chapter 8).
kdb shell: type SQL
kdb shell <folder> opens a prompt. Type SQL and end it with ;. A statement can use many lines. The prompt ...> means "the statement is not finished yet".
PS D:\kdb-work> kdb shell market
KDB 0.2.0-dev shell on market
End statements with ';'. Type .help for commands, .exit to quit.
kdb> .help
.tables list tables
.schema <table> show CREATE TABLE
.format table|csv|json
.maxrows <n> rows shown per query (0 = all)
.check read every file and report damage
.backup make a verified backup (needs --backups)
.exit quit
kdb> .tables
name | kind | partition | columns | rows
-------------+------------+-----------+---------+-----
brokers | relational | NONE | 2 | 5
trade_log | timeseries | DAY | 8 | 32
live_data | timeseries | DAY | 12 | 12
ochl_day | timeseries | YEAR | 7 | 57
ochl_minutes | timeseries | MONTH | 7 | 956
securities | relational | NONE | 3 | 5
(6 rows, 0.0 ms)
kdb> SELECT symbol, close
...> FROM ochl_day
...> WHERE time IN '2026-09-10';
symbol | close
-------+------
CITY | 504.9
COAST | 376.1
VOLT | 191.2
(3 rows, 0.4 ms)
kdb> .format csv
kdb> .schema brokers
create_sql
"CREATE TABLE brokers (
id INT PRIMARY KEY,
name STRING NOT NULL
)"
kdb> SELECT id, name FROM brokers LIMIT 3;
id,name
1,Keystone Securities
34,Vision Securities
45,Imperial Securities
kdb> .exit
Shell commands
Shell commands start with a dot. They do not need ;.
| Command | What it does |
|---|---|
.help | Shows the commands |
.tables | Lists the tables (same as SHOW TABLES) |
.schema <table> | Shows the CREATE TABLE statement of a table |
.format table / csv / json | How results are printed. table is the default. |
.maxrows <n> | Shows at most n rows per query (default 1000). 0 = all rows. |
.check | Reads every file and reports damage |
.backup | Makes a backup. Start the shell with --backups <folder> first. |
.exit or exit | Quits the shell. Ctrl+C also works. |
Tips:
- In
tableformat, long text is cut after 48 characters (...). Use.format csvto see all of it, for example for.schema. - Keywords and names do not care about case:
select=SELECT,OCHL_DAY=ochl_day. - Comments:
-- to the end of the lineand/* inside */.
kdb sql: run SQL once
kdb sql <folder> "SQL" opens the database, runs the SQL, prints the result and closes the database. This is good for scripts.
kdb sql market "SELECT count(*) AS minute_rows FROM ochl_minutes"
minute_rows
-----------
956
(1 row, 0.5 ms)
Many statements
Separate statements with ;. KDB prints one result for each statement, with an empty line between them.
kdb sql market "SELECT count(*) AS securities FROM securities; SELECT count(*) AS brokers FROM brokers"
securities
----------
5
(1 row, 0.2 ms)
brokers
-------
5
(1 row, 0.1 ms)
If one statement fails, KDB stops there. The statements before it are already saved. The statements after it do not run. See chapter 4.
SQL from a file
Write the SQL in a file. Then send the file into kdb sql with a pipe. The - means "read the SQL from the pipe".
@"
-- report.sql: closing prices on the last day
SELECT symbol, close
FROM ochl_day
WHERE time IN '2026-09-10'
ORDER BY close DESC;
"@ | Set-Content report.sql -Encoding UTF8
Get-Content report.sql -Raw | kdb sql market -
symbol | close
-------+------
CITY | 504.9
COAST | 376.1
VOLT | 191.2
(3 rows, 0.2 ms)
- Use
-Raw. It sends the file as one piece of text. - If the file has text in another language (not English), see chapter 7.
- In
cmd.exeand bash you can writekdb sql market - < report.sql.
Options of kdb sql
| Option | Meaning |
|---|---|
--format table | Text table (default) |
--format csv | CSV, good for Excel and scripts |
--format json | JSON, good for programs |
--max-rows N | At most N rows per statement. Default: 1000 for table, no limit for csv and json. |
--backups <folder> | Backup folder, needed for BACKUP DATABASE |
Chapter 7 shows the formats in detail.
Exit codes
Every kdb command ends with an exit code. Scripts use it to know if the command worked.
| Code | Meaning | Examples |
|---|---|---|
0 | OK | The query ran. The backup was made. No damage found. |
1 | Error | Wrong SQL. Duplicate key. The database is open in another program. Wrong arguments. |
2 | Damage | kdb check found damage. A file could not be read. A repair could not fix everything. |
3 | Needs a newer KDB | The database uses a stored-format feature this kdb does not know. Nothing was changed. See chapter 24. |
In PowerShell the exit code is in $LASTEXITCODE:
kdb sql market "SELECT count(*) AS n FROM brokers"
"exit code: $LASTEXITCODE"
kdb sql market "SELECT count(*) FROM brokerz"
"exit code: $LASTEXITCODE"
n
-
5
(1 row, 0.2 ms)
exit code: 0
error: line 1, column 22: table or view 'brokerz' does not exist
exit code: 1
Error messages say where the problem is: line 1, column 22 is the place of brokerz in the SQL. In bash the exit code is in $?.
kdb shell ends with the exit code of the last statement or command you ran.
Try it yourself
- Open the shell on
marketand list the columns oflive_data. - With one
kdb sqlcommand, count the rows oftrade_logandlive_data. - Run
kdb sql market "SELECT * FROM nothing". What is the exit code?
Answers
kdb> SHOW COLUMNS FROM live_data;(or
.schema live_dataafter.format csv)kdb sql market "SELECT count(*) AS trades FROM trade_log; SELECT count(*) AS snapshots FROM live_data"The answers are 32 and 12.
- The exit code is
1, because the table does not exist.
Next: 3. Tables