USQL(1)

πŸ“¦ version 0.19.19, in ubuntu resolute, ubuntu stonking

πŸ¦‹ NAME πŸ¦‹

okay so bestie, meet usql β€” a universal command-line interface for SQL, NoSQL, and other databases. yes, universal. it does the most, in the best way. πŸ’…


✨ DESCRIPTION ✨

usql is a universal command-line interface for PostgreSQL, MySQL, MariaDB, Oracle Database, TiDB, SQLite3, Microsoft SQL Server, and so many other databases β€” including NoSQL and non-relational databases! it was Inspired by PostgreSQL's powerful command-line tool psql. like, inspired-inspired. 🫢

usql gives you a simple way to interact with a whole bunch of database systems through one consistent interface. it supports most core psql features β€” variables, backticks, and backslash commands, all of it β€” while adding unique capabilities like syntax highlighting, context-based completion, native multi-database support via standard URL connection strings, copying data between different database systems, terminal graphics support, and more. the way it just does all that? iconic. πŸ‘‘

Database administrators and developers who prefer a psql-like experience for non-PostgreSQL databases will find usql intuitive and powerful. it supports connecting to various database drivers (see the \drivers command within usql), executing SQL commands, running scripts from files, and formatting output in various styles (aligned, unaligned, HTML, JSON, CSV, vertical, etc.). pick a vibe, any vibe. 🌈


πŸ’– OPTIONS πŸ’–

  • -c, --command COMMAND β€” run only single command (SQL or internal) and exit. one and done, periodt.
  • -f, --file FILE β€” execute commands from file and exit
  • -w, --no-password β€” never prompt for password
  • -X, --no-init β€” do not execute initialization scripts (aliases: --no-rc --no-psqlrc --no-usqlrc)
  • -o, --out FILE β€” output file
  • -W, --password β€” force password prompt (should happen automatically)
  • -1, --single-transaction β€” execute as a single transaction (if non-interactive)
  • -v, --set NAME=VALUE β€” set variable NAME to VALUE (see \set command, aliases: --var --variable)
  • -N, --cset NAME=DSN β€” set named connection NAME to DSN (see \cset command)
  • -P, --pset VAR=ARG β€” set printing option VAR to ARG (see \pset command)
  • -F, --field-separator FIELD-SEPARATOR β€” field separator for unaligned and CSV output (default "|" and ",")
  • -R, --record-separator RECORD-SEPARATOR β€” record separator for unaligned and CSV output (default \n)
  • -T, --table-attr TABLE-ATTR β€” set HTML table tag attributes (e.g., width, border)
  • -A, --no-align β€” unaligned table output mode
  • -H, --html β€” HTML table output mode
  • -t, --tuples-only β€” print rows only
  • -x, --expanded β€” turn on expanded table output
  • -z, --field-separator-zero β€” set field separator for unaligned and CSV output to zero byte
  • -0, --record-separator-zero β€” set record separator for unaligned and CSV output to zero byte
  • -J, --json β€” JSON output mode
  • -C, --csv β€” CSV output mode
  • -G, --vertical β€” vertical output mode
  • -q, --quiet β€” run quietly (no messages, only query output)
  • --config string β€” config file
  • -V, --version β€” output version information, then exit
  • -?, --help β€” Show this help, then exit.

🌸 CONNECTING TO DATABASES 🌸

usql connects to databases using Data Source Name (DSN) URLs, typically in the format:

driver[+transport]://[user[:pass]@][host][:port][/dbname][?opt1=val1&opt2=val2]

Where:

  • driver β€” Is the primary scheme or alias for the database driver (e.g., postgres, pg, mysql, my, sqlserver, ms, oracle, or, sqlite3, sq, csvq). Use the \drivers command in usql to see available drivers and aliases.
  • transport β€” Is optional, usually tcp, udp, or unix. Some drivers like ODBC use it for sub-protocol specification.
  • user, pass, host, port β€” Are standard connection credentials. Many parts are optional and defaults may apply (e.g., connecting via local Unix sockets).
  • dbname β€” Represents the database name, service ID, instance, or sometimes a file path (for file-based databases like SQLite3 or DuckDB). For some drivers (e.g., SQL Server, Oracle), this can be /instance/dbname.
  • ?opt1=val1... β€” Are driver-specific connection options passed as URL query parameters.

usql can also connect to local files directly (e.g., /path/to/db.sqlite3) or named connections defined in the configuration file. lowkey convenient. πŸŽ€

See the project's README.md for detailed examples and supported drivers.


πŸ’… COMMANDS πŸ’…

usql interprets backslash (\) commands for meta-operations, similar to psql. These provide functionality beyond standard SQL. Use \? within usql for a full list. Key commands include:

  • \c, \connect DSN | NAME β€” Connect to a database using a DSN URL or a pre-defined named connection.
  • \copy SRC_DSN DST_DSN QUERY TABLE[(COLS)] β€” Copy data between databases. Executes QUERY on SRC_DSN and inserts results into TABLE on DST_DSN.
  • \d[+] [PATTERN] β€” List tables, views, sequences. Add '+' for more detail. Many variants exist (\dt, \di, \f, n, etc.) to list specific object types. Use \? options for details.
  • \g, \go [(OPTIONS)] [FILE] β€” Execute the current query buffer, optionally sending results to a FILE or pipe.
  • \G, \ego [(OPTIONS)] [FILE] β€” Execute query and force vertical output mode.
  • \gx [(OPTIONS)] [FILE] β€” Execute query and force expanded output mode.
  • \i, \include FILE β€” Execute commands from FILE.
  • \p, \print [-raw|-exec] β€” Show the contents of the query buffer (or the raw/exec buffer).
  • \r, \reset β€” Clear the query buffer.
  • \set [NAME [VALUE]] β€” Set or show runtime variables. Used for query interpolation.
  • \pset [NAME [VALUE]] β€” Set or show display formatting options (e.g., border, format, tuples_only).
  • \cset [NAME [DSN]] β€” Set or show named connection DSNs.
  • \timing [on|off] β€” Toggle display of command execution time.
  • \! [COMMAND] β€” Execute a command in the system shell.
  • \q, \quit β€” Quit usql.

πŸ¦‹ VARIABLES πŸ¦‹

usql utilizes variables for configuration and dynamic query generation:

  • Runtime Variables (\set) β€” Set with \set NAME VALUE. Interpolated into queries using :NAME (direct substitution), :'NAME' (quoted as string literal), or :"NAME" (quoted as identifier). View all with \set. Unset with \unset NAME.
  • Connection Variables (\cset) β€” Set with \cset NAME DSN. Provide aliases for database connection URLs used with \c or \copy. View all with \cset. Not used for query interpolation.
  • Display Formatting Variables (\pset) β€” Set with \pset NAME VALUE or toggle commands like \a, \H, \x, \t. Control table borders, output formats (aligned, csv, html, json, etc.), titles, timing display, etc. View all with \pset.
  • Special variables (set via \set) control behavior like syntax highlighting (SYNTAX_HL, SYNTAX_HL_STYLE), host info display (SHOW_HOST_INFORMATION), prompts (PROMPT1, PROMPT2, PROMPT3), etc. Use \? variables in usql for a comprehensive list.

πŸŽ€ FILES πŸŽ€

  • $HOME/.config/usql/config.yaml β€” Primary configuration file (path varies by OS: %AppData%/usql/config.yaml on Windows, $HOME/Library/Application Support/usql/config.yaml on macOS). Defines named connections (connections:), startup commands (init:), default settings, etc. Uses YAML format.
  • $HOME/.usqlrc β€” Legacy startup script executed when usql starts interactively, unless -X is given. Useful for setting variables or running initial commands. The init: section in config.yaml is now preferred.
  • $HOME/.usqlpass β€” Legacy file for storing database passwords (permissions must be 0600). Format: protocol:host:port:dbname:user:pass. Using named connections in config.yaml is generally safer and more flexible.
ngl bestie, those last two are giving legacy energy β€” config.yaml is the main character now. πŸ‘‘

🌈 ENVIRONMENT 🌈

  • EDITOR β€” Specifies the editor used by the \e command.
  • PAGER β€” Specifies the pager program used for displaying help (\?) and other long output.
  • SHELL β€” Specifies the shell used by the \! command.
  • USQL_CONFIG_DIR β€” Overrides the default directory for configuration files (config.yaml, .usqlrc, .usqlpass).
  • USQL_SHOW_HOST_INFORMATION β€” Set to false to prevent displaying database host/version information upon connection. Can also be controlled by the SHOW_HOST_INFORMATION variable via \set.
  • USQL_TERM_GRAPHICS β€” Force enable or disable terminal graphics support. Values: kitty, iterm, sixel, none. Overrides automatic detection.
  • TERM_GRAPHICS β€” Alternative to USQL_TERM_GRAPHICS.

πŸ”₯ EXAMPLES πŸ”₯

Connect to a PostgreSQL database interactively:

usql pg://user:pass@localhost:5432/mydatabase

Connect to a local SQLite3 file (will create if not exists if scheme is present):

usql sqlite3://./data.db

Run a single SQL command against a MySQL database and exit:

usql -c SELECT user, host FROM mysql.user; my://root@localhost/

Execute commands from a script file against SQL Server:

usql -f setup.sql ms://sa:Password123@server.example.com/master

Connect using a named connection 'prod_db' defined in config.yaml:

usql prod_db

Set a variable on the command line and use it in a query:

usql -v SCHEMA=public -v TBL=users pg://... -c 'SELECT * FROM :"SCHEMA".:"TBL" LIMIT 10;'

Copy all data from a PostgreSQL table 'source_tbl' to an Oracle table 'TARGET_TBL':

usql -c \copy pg://... or://... 'SELECT * FROM source_tbl' 'TARGET_TBL'

Connect to a CSV file in the current directory and query it:

usql csvq://. -c SELECT column_a, column_b FROM 'data.csv' WHERE column_a > 10;

⭐ SEE ALSO ⭐

The full documentation, source code, and issue tracker can be found on GitHub:

https://github.com/xo/usql

okay that's the whole thing, you're literally so ready. πŸ’– this was usql 0.19.19, from April 2025, and yes it's the USQL(1) page β€” go slay some queries, bestie. πŸ«Άβœ¨πŸŽ€

πŸ“¦ where this page comes from