Skip to content

CLI

Terminal window
npm install -g @petradb/cli
Terminal window
petradb [OPTIONS] [path]

If a path is given, the CLI opens (or creates) a persistent database at that location. Without a path, an in-memory database is used.

OptionDescription
-m, --memoryUse an in-memory database (even if a path is given)
-e, --executeExecute SQL and exit (repeatable)
-f, --fileExecute a SQL file and exit (repeatable)
--stdinRead SQL from stdin and exit
--hostConnect to a remote PetraDB server
--portServer port (default: 5480)
--userUsername for authentication
--passwordPassword for authentication
Terminal window
# Start an interactive in-memory session
petradb
# Open or create a persistent database
petradb mydata.db
# Run SQL non-interactively
petradb mydata.db -e "SELECT * FROM users"
# Execute a file then a query
petradb mydata.db -f schema.sql -e "SELECT count(*) FROM users"
# Pipe SQL from stdin
echo "SELECT 1 + 1" | petradb --stdin
# Connect to a remote server
petradb --host myserver.example.com --port 5480
# Connect with authentication
petradb --host localhost --user admin --password secret

The file extension determines the storage mode:

ExtensionModeDescription
.petra (or any other)PersistentCrash-safe durable storage via copy-on-write pages
.ptxtTextHuman-readable file, rewritten after every change
(no path)In-memoryData lives only for the duration of the session

The dump subcommand prints the full schema and data of a persistent database as SQL statements.

Terminal window
petradb dump mydata.db

Output includes CREATE TYPE, CREATE TABLE, INSERT INTO, and CREATE VIEW statements that can recreate the database from scratch.

When started without -e, -f, or --stdin, the CLI enters an interactive SQL shell with readline history.

Type SQL terminated by ; to execute. Multi-line input is supported — a continuation prompt ( ->) appears until you end with ;.

CommandDescription
\dtList all tables
\dvList all views
\dsList all sequences
\diList all indexes
\d <name>Describe a table or view (columns, types, nullability)
\i <file>Execute SQL from a file
\dumpPrint the full schema and data as SQL
\copy <args>Run a COPY statement
\timingToggle query timing on/off
\qQuit
$ petradb
PetraDB — interactive SQL shell
Type \q to quit, \dt to list tables, \d <table> to describe a table.
petra> CREATE TABLE cities (name TEXT, population INT);
CREATE TABLE
petra> INSERT INTO cities (name, population) VALUES ('Oslo', 709037);
INSERT 0 1
petra> SELECT * FROM cities;
name | population
------+------------
Oslo | 709037
(1 row)
petra> \dt
Table
--------
cities
(1 row)
petra> \d cities
Table "cities"
Column | Type | Nullable
------------+------+----------
name | TEXT | null
population | INT | null
petra> \timing
Timing is on.
petra> SELECT count(*) FROM cities;
count
-------
1
(1 row)
Time: 0.002 s
petra> \q