Skip to content

NikolayS/rpg

Repository files navigation

rpg — modern Postgres terminal written in Rust

CI codecov License Rust

A psql-compatible terminal written in Rust with built-in DBA diagnostics and AI assistant. Single binary, no dependencies, cross-platform.

Features

  • psql-compatible — drop-in replacement (\d, \dt, \copy, \watch, ...)
  • AI assistant/ask, /fix, /explain, /optimize
  • DBA diagnostics — 15+ \dba commands for activity, locks, bloat, indexes
  • Schema-aware completion — tab completion for tables, columns, keywords
  • TUI pager — scrollable pager for large result sets
  • Syntax highlighting — SQL keywords, strings, operators; color-coded errors (red), warnings (yellow), notices (cyan)
  • Named queries — save and recall frequent queries
  • Session persistence — history and settings preserved across sessions
  • Config profiles — per-project .rpg.toml
  • Status bar — connection info, transaction state, timing
  • Cross-platform — single static binary: Linux, macOS, Windows (x86_64 + aarch64)

Installation

Build from source (requires Rust 1.85+):

git clone https://github.com/NikolayS/rpg.git
cd rpg
cargo build --release
sudo cp ./target/release/rpg /usr/local/bin/

Connect

# Same flags as psql
rpg -h localhost -p 5432 -U postgres -d mydb

# Connection string
rpg "postgresql://user@localhost/mydb"

# Non-interactive
rpg -d postgres -c "select version()"

AI assistant

Integrates with OpenAI, Anthropic, and Ollama:

-- Ask questions about your database
/ask What indexes should I add for my orders table?

-- Interpret EXPLAIN (ANALYZE, BUFFERS) output
select * from orders where status = 'pending';
/explain

-- Fix errors and optimize queries
/fix
/optimize

\text2sql — natural language to SQL

By default, the generated SQL is shown in a preview box and you confirm before it runs:

postgres=# \text2sql
Input mode: text2sql
postgres=# what is DB size?
┌── sql
select pg_size_pretty(pg_database_size(current_database())) as db_size;
└───────
Execute? [Y/n/e]
 db_size
---------
 58 MB
(1 row)

\yolo — fast natural-language mode

\yolo combines text2sql and silent auto-execute in one command: it enables text2sql input, hides the SQL preview box, and executes immediately without confirmation.

postgres=# \yolo
Execution mode: yolo
postgres=# what is DB size?
 db_size
---------
 58 MB
(1 row)

Toggle back with \sql or \interactive. Show/hide the SQL preview box with \set TEXT2SQL_SHOW_SQL on.

\t2s text-to-SQL mode and \yolo auto-execute mode in action

\t2s shows a SQL preview with confirmation; \yolo skips the preview and executes immediately.

/fix — auto-correct errors

postgres=# select * fromm t1 where i = 10;
ERROR:  syntax error at or near "fromm"
LINE 1: select * fromm t1 where i = 10;
                 ^
Hint: Replace "fromm" with "from".
Hint: type /fix to auto-correct this query

postgres=# /fix
Corrected SQL query:
┌── sql
select * from t1 where i = 10;
└───────
Execute? [Y/n/e]
  i |             random
----+--------------------
 10 | 0.6895257944299762
(1 row)

/fix auto-corrects a typo in the table name and re-runs the query

/fix detects a misspelled table name, suggests the corrected query, and executes it after confirmation.

/optimize — index and performance suggestions

postgres=# /optimize
<runs EXPLAIN (ANALYZE, BUFFERS), then suggests:>

1. Create an Index on t1.i — parallel seq scan is inefficient for point lookups
   CREATE INDEX idx_t1_i ON public.t1 (i);
   Expected: 28ms → sub-millisecond

2. Run ANALYZE on t1 — statistics may be stale
   ANALYZE public.t1;

/explain and /optimize workflow: slow query, AI analysis, index creation, re-run

/explain interprets the query plan; /optimize suggests an index. After creating it, the same query runs dramatically faster.

psql-compatible display settings

\pset — display settings

postgres=# \pset null '∅'
Null display is "∅".
postgres=# select id, name, deleted_at from users limit 3;
 id | name  | deleted_at
----+-------+------------
  1 | Alice | ∅
  2 | Bob   | 2024-03-15
  3 | Carol | ∅
(3 rows)

External pager (pspg)

pspg is supported as an external pager:

\set PAGER pspg

Or set PAGER=pspg in your environment before launching rpg.

DBA diagnostics

15+ diagnostic commands accessible via \dba:

postgres=# \dba
  \dba activity    pg_stat_activity: grouped by state, duration, wait events
  \dba locks       Lock tree (blocked/blocking)
  \dba waits       Wait event breakdown (+ for AI interpretation)
  \dba bloat       Table bloat estimates
  \dba vacuum      Vacuum status and dead tuples
  \dba tablesize   Largest tables
  \dba connections Connection counts by state
  \dba indexes     Index health (unused, redundant, invalid, bloated)
  \dba seq-scans   Tables with high sequential scan ratio
  \dba cache-hit   Buffer cache hit ratios
  \dba replication Replication slot status
  \dba config      Non-default configuration parameters
  \dba progress    Long-running operation progress (pg_stat_progress_*)
  \dba io          I/O statistics by backend type (PG 16+)

Index health example:

postgres=# \dba indexes
Index health: 2 issues found.

!  [unused] public.orders
   index: orders_status_idx  (0 scans since stats reset, 16 KiB)
   suggestion: DROP INDEX CONCURRENTLY public.orders_status_idx

SSH tunnel

Connect through an SSH bastion with no extra tooling:

rpg --ssh-tunnel user@bastion.example.com -h 10.0.0.5 -d mydb

PostgreSQL compatibility

Supports PostgreSQL 14, 15, 16, 17, and 18.

Development

rpg is engineered by Nikolay Samokhvalov with a team of Claude Opus 4.6 AI agents (via Claude Code, occasionally with OpenClaw). All architecture decisions, feature design, and project direction are human-driven. The codebase is ~46 kLOC and 100% of the code has been AI-reviewed and CI/AI-tested, though only a portion has been manually reviewed line-by-line.

License

Apache 2.0 — see LICENSE.

About

rpg — modern Postgres terminal written in Rust. psql-compatible, with built-in DBA diagnostics and AI assistant

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors