# Databases and the data folder > Where KDB keeps databases, how to name them, and CREATE DATABASE, DROP DATABASE, SHOW DATABASES and USE. - Category: Databases & folders - Keywords: database, data folder, PURGE TRASH, PURGE TRASH ALL, data directory, --data-dir, KDB\_DATA\_DIR, default database, \_trash, trash, database name, folder path, CREATE DATABASE, IF NOT EXISTS, DROP DATABASE, IF EXISTS, SHOW DATABASES, USE, switch database, many databases, kdb.lock, wal\_archive, WAL\_ARCHIVE, old log, log archive, KDB\_WAL\_ARCHIVE\_MB, RESULT\_CACHE, result cache per database, MEMORY\_LIMIT, MEMORY\_RESERVED, memory per database, database\_memory, per-column loads, partial\_loads, BRANCH OF, branch, clone database - See also: [Quick start](quick-start.md), [CREATE TABLE](create-table.md), [SHOW TABLES, COLUMNS, CREATE TABLE](show-tables.md), [Users and roles](users-and-roles.md), [Backup, check, repair and restore](backup-and-safety.md), [kdb serve](cli-serve.md), [Queries across databases](cross-database-queries.md), [Time travel and branches: SELECT ... AS OF, CREATE DATABASE ... BRANCH OF](as-of-and-branches.md) A **database** is one folder with its own tables, views, triggers and procedures. KDB keeps many databases in one **data folder**: one sub-folder per database. `kdb serve` without a database name serves all of them. A real setup: one database for market prices, one for the accounting app, one for the forum. **Example: Three databases for three apps** ```sql CREATE DATABASE market; CREATE DATABASE accounts; CREATE DATABASE forum; SHOW DATABASES; ``` Result: `CREATE DATABASE market` ```text database 'market' created ``` `CREATE DATABASE accounts` ```text database 'accounts' created ``` `CREATE DATABASE forum` ```text database 'forum' created ``` `SHOW DATABASES` | name (STRING) | tables (LONG) | bytes (LONG) | current (BOOL) | |---|---|---|---| | accounts | 0 | 0 | false | | demo | 0 | 0 | true | | forum | 0 | 0 | false | | market | 0 | 0 | false | (4 rows) ## The data folder Syntax: ```sql kdb --data-dir ... KDB_DATA_DIR= \data ``` KDB looks for the data folder in this order. The first one that is set wins: | # | Where | Example | |---|---|---| | 1 | the option `--data-dir ` | `kdb serve --data-dir E:\kdb-data` | | 2 | the environment variable `KDB_DATA_DIR` | `$env:KDB_DATA_DIR = "E:\kdb-data"` | | 3 | the folder `data` next to the `kdb` program | `D:\KDB\build\bin\data` | KDB creates the folder when it is missing. ```text E:\kdb-data\ <- the data folder E:\kdb-data\default\ <- the database "default" (kdb shell without a name uses it) E:\kdb-data\market\ <- the database "market" E:\kdb-data\_trash\ <- dropped databases (kept until PURGE TRASH ALL) ``` - **Notes:** use `KDB_DATA_DIR` when the data must be on another disk, or when `kdb.exe` is in a folder where you cannot write (like `C:\Program Files`). To set it for good on Windows: `[Environment]::SetEnvironmentVariable("KDB_DATA_DIR", "E:\kdb-data", "User")`, then open a new PowerShell window. - **Notes:** never change files inside a database folder by hand. Only one program can open a database at a time (the file `kdb.lock` shows this). **Command line: A shell on a data folder: make a database and list them** ```bash kdb shell --data-dir my-data > CREATE DATABASE market; > SHOW DATABASES; ``` Output: ```text KDB 0.2.0-dev shell on default (data folder my-data) End statements with ';'. Type .help for commands, .exit to quit. kdb> database 'market' created kdb> name | tables | bytes | current --------+--------+-------+-------- default | 0 | 0 | true market | 0 | 0 | false (2 rows, 0.5 ms) kdb> ``` ## Database names and folder paths Syntax: ```sql name letters, digits, _ and - (1 to 64 characters) .\folder a path: always that folder ``` A database name has only letters `a-z A-Z`, digits, `_` and `-`, and is 1 to 64 characters long. Upper and lower case do not matter: `Market` and `market` are the same database. The folder keeps the spelling you used first. | Not allowed | Why | |---|---| | `my shop`, `my.shop`, `नेपाल` | only letters a-z, digits, `_` and `-` | | `_trash` | the folder for dropped databases | | `_system` | the folder where KDB keeps its users | | `CON`, `PRN`, `AUX`, `NUL`, `COM0`-`COM9`, `LPT0`-`LPT9` | device names on Windows | Commands like `kdb sql` and `kdb shell` take a **name** or a **path**: | You write | KDB uses | |---|---| | a name, and the data folder has that database | that database | | a name, and the data folder does not have it | the folder with that name in the **current** folder (made when missing) | | a path with `\`, `/`, `:` or `.` (`.\market`, `D:\kdb\market`) | always that folder | - **Notes:** a command that opened a database by its **path** cannot create or drop databases. SQL names in quotes do not help: `CREATE DATABASE "my shop"` is still refused. **Example: A name with a dot** (shows an error) ```sql CREATE DATABASE my.shop; ``` Result: Error: `line 1, column 19: unexpected '.' after CREATE DATABASE` **Example: A reserved folder name** (shows an error) ```sql CREATE DATABASE _trash; ``` Result: Error: `line 1, column 17: bad database name '_trash': '_trash' is where dropped databases are kept` **Command line: A name that is not in the data folder means a folder here** ```bash kdb sql shop "CREATE TABLE products (id INT PRIMARY KEY, name STRING)" kdb sql .\shop "SHOW TABLES" ``` Output: `> kdb sql shop "CREATE TABLE products (id INT PRIMARY KEY, name STRING)"` ```text table 'products' created ``` `> kdb sql .\shop "SHOW TABLES"` ```text name | kind | partition | columns | rows ---------+------------+-----------+---------+----- products | relational | NONE | 2 | 0 (1 row, 0.1 ms) ``` ## CREATE DATABASE Syntax: ```sql CREATE DATABASE [IF NOT EXISTS] name [WITH (pg_compat = on | off, encryption = on | off)] ``` Makes a new, empty database folder in the data folder. With `IF NOT EXISTS`, a database that is already there is not an error. | Argument | Type | Meaning | |---|---|---| | `name` | database name | see [names](#names) | | `pg_compat` | on / off | PostgreSQL 14's answers in this database ([PostgreSQL protocol](postgres-protocol.md)) | | `encryption` | on / off | every file of the database is encrypted; needs the server's master key ([encryption at rest](encryption.md)). Default `off` | - **Returns:** the message `database 'name' created` (`... created encrypted` with `encryption = on`), or `database 'name' already exists` with `IF NOT EXISTS`. - **Notes:** `IF NOT EXISTS` never changes a database that is there: a plain one stays plain. Encryption is chosen only here; `ALTER DATABASE` cannot switch it. - **Notes:** needs a data folder: it works in `kdb shell`, in `kdb sql` with a database name, and in `kdb serve` started without a folder path. With users, only a user with `ROLE admin` may run it ([users and roles](users-and-roles.md)). - **Notes:** an empty folder with that name becomes the database. A folder with other files in it is refused. - **Notes:** `CREATE DATABASE test BRANCH OF live [AS OF ...]` makes a full, writable copy of another database in a moment, sharing its files ([branches](as-of-and-branches.md#branch)). **Example: Safe to run again with IF NOT EXISTS** ```sql CREATE DATABASE IF NOT EXISTS accounts; CREATE DATABASE IF NOT EXISTS accounts; SHOW DATABASES; ``` Result: `CREATE DATABASE IF NOT EXISTS accounts` ```text database 'accounts' created ``` `CREATE DATABASE IF NOT EXISTS accounts` ```text database 'accounts' already exists ``` `SHOW DATABASES` | name (STRING) | tables (LONG) | bytes (LONG) | current (BOOL) | |---|---|---|---| | accounts | 0 | 0 | false | | demo | 0 | 0 | true | (2 rows) **Example: Upper and lower case are the same database** (shows an error) ```sql CREATE DATABASE Forum; CREATE DATABASE forum; ``` Result: ```text database 'Forum' created ``` Error: `database 'Forum' already exists` ## DROP DATABASE Syntax: ```sql DROP DATABASE [IF EXISTS] name ``` Closes the database and **moves** its folder to `/_trash/