sqltree
sqltree is an interactive Terminal User Interface (TUI) client designed for seamless navigation and interaction with PostgreSQL and MySQL databases. It currently ships with version 1.4.7, with updates primarily driven by dependency bumps and feature enhancements, indicating active development. The tool differentiates itself through a two-column layout featuring a navigable tree browser for databases, schemas, and tables on one side, and a full-screen SQL REPL with auto-completion on the other. It supports paginated data browsing, result export to CSV/JSON, and persistent connection profiles, all within a unified interface for both database types. sqltree runs on Node.js 20 or higher.
Common errors
-
sqltree: command not found
cause The `sqltree` command is not available in your system's PATH. This typically happens if the package is not installed globally or `npx` is not found.fixRun `sqltree` using `npx sqltree` to execute it without a global installation, or install it globally with `npm install -g sqltree`. -
Error: connect ECONNREFUSED ::1:5432
cause The `sqltree` client could not establish a connection to the specified database server. This is commonly due to an incorrect host/port, the database server not running, or network/firewall issues.fixVerify that your database server is running, check the host address and port in your connection string or arguments, and ensure no firewall rules are blocking the connection from your machine. -
Error: Cannot find module 'pg' or Error: Cannot find module 'mysql2'
cause A required database driver is missing or could not be loaded. This might occur if `npx`'s dependency resolution fails or if `sqltree` was installed in an incomplete environment.fixEnsure you have a stable internet connection for `npx` to download dependencies. If `sqltree` is installed globally, try reinstalling it with `npm install -g sqltree`. For programmatic usage via `child_process`, ensure `npx` is in your PATH.
Warnings
- gotcha Connection profiles, which may contain sensitive database passwords, are saved to `~/.sqltree/connections.json`. This file has restricted permissions (mode `0600`), but users must still treat it as highly sensitive.
- breaking sqltree requires Node.js version 20 or higher to run. Executing the tool with older Node.js versions will lead to errors or unexpected behavior.
- gotcha sqltree is a command-line interface application and does not expose a programmatic API. Attempting to `import` or `require` the `sqltree` package in your Node.js or browser applications will result in a module not found or undefined error.
Install
-
npm install sqltree -
yarn add sqltree -
pnpm add sqltree
Imports
- sqltree (npx invocation)
import sqltree from 'sqltree'
npx sqltree
- sqltree (global command)
require('sqltree')npm install -g sqltree sqltree
- sqltree (with connection URI)
sqltree.connect({ uri: '...' })npx sqltree --uri postgresql://user:pass@host:port/database
Quickstart
const { spawn } = require('child_process');
// Example: Connect to a PostgreSQL database using a URI from an environment variable.
// This demonstrates programmatically launching the sqltree CLI.
// Replace 'your_user', 'your_password', 'localhost', '5432', 'your_database_name' with actual values.
// For security, it's recommended to use environment variables for sensitive data.
const DB_URI = process.env.SQLTREE_PG_URI ?? 'postgresql://your_user:your_password@localhost:5432/your_database_name';
console.log(`Launching sqltree to connect to: ${DB_URI.replace(/:[^@]+@/, ':*****@')}`); // Mask password for logging
const child = spawn('npx', ['sqltree', '--uri', DB_URI], {
stdio: 'inherit', // Allows sqltree's TUI to render in the current terminal
shell: true // Required for 'npx' command to be found on some systems
});
child.on('error', (err) => {
console.error('Failed to start sqltree process:', err);
});
child.on('close', (code) => {
if (code !== 0) {
console.error(`sqltree process exited with code ${code}`);
} else {
console.log('sqltree process exited successfully.');
}
});