LinuxCommandLibrary

usql

Connect and execute queries against databases

TLDR

Connect to a specific database

$ usql [sqlserver|mysql|postgres|sqlite3|...]://[username]:[password]@[host]:[port]/[database_name]
copy

Execute commands from a file
$ usql --file=[path/to/query.sql]
copy

Execute a specific SQL command
$ usql --command="[sql_command]"
copy

Run an SQL command in the usql prompt
$ [prompt]=> [command]
copy

Display the database schema
$ [prompt]=> \d
copy

Export query results to a specific file
$ [prompt]=> \g [path/to/file_with_results]
copy

Import data from a CSV file into a specific table
$ [prompt]=> \copy [path/to/data.csv] [table_name]
copy

SYNOPSIS

usql [options] [dsn...]
usql [options] file

PARAMETERS

-c, --command
    Execute a single command string and exit.

-f, --file
    Execute commands from a specified file.

-v, --variable
    Set a usql variable (e.g., VAR=value).

-X, --no-rc
    Do not read the start-up file (typically ~/.usqlrc).

-p, --password
    Force usql to prompt for a password before connecting.

-P, --no-password
    Never prompt for a password.

-h, --help
    Show help message and exit.

-V, --version
    Display version information and exit.

-S, --no-pager
    Disable use of a pager for command output.

-q, --quiet
    Run in quiet mode, suppressing most messages.

-E, --echo-queries
    Echo SQL queries sent to the server.

-e, --echo-commands
    Echo commands (including meta-commands) before execution.

-l, --list
    List all available database drivers and exit.

-d, --dsn
    Specify a Data Source Name for the connection (can be repeated).

-o, --output
    Send query output to a specified file.

-H, --no-header
    Do not print column header names.

-J, --json
    Format query output as JSON.

--csv
    Format query output as CSV.

--tsv
    Format query output as TSV.

-A, --no-align
    Produce unaligned output (tab-separated by default).

-F, --field-separator
    Set the field separator for unaligned output.

-R, --record-separator
    Set the record separator for unaligned output.

-t, --tuples-only
    Show only rows of data, suppressing headers, footers, and other information.

-x, --expanded
    Enable expanded output mode (each row is printed on multiple lines).

-U, --user
    Specify the username for the connection.

-C, --color
    Force color output, even if not connected to a TTY.

-N, --no-auto-completion
    Disable auto-completion.

-m, --meter
    Display a progress meter for long-running queries.

-r, --recursive
    Recursively search for configuration files (e.g., ~/.usqlrc).

-s, --single-line
    Enter single-line mode (newline terminates SQL command).

-O, --option
    Set a usql option.

-i, --init-file
    Read and execute commands from a file after connecting.

-w, --write
    Write mode: print responses to stdout and exit. Useful for scripting.

DESCRIPTION

usql is a universal command-line interface for working with a wide range of SQL databases. Built upon Go's database/sql package, it offers a consistent interactive experience across various database systems like PostgreSQL, MySQL, SQLite3, SQL Server, Oracle, and many more. It provides powerful features such as auto-completion for SQL keywords and database objects, syntax highlighting, and a rich set of meta-commands (like \dt to list tables).

usql aims to abstract away the specific CLI tools for each database, providing a single, unified interface for developers and DBAs. Its extensible architecture allows it to support new database drivers easily. This makes usql an indispensable tool for anyone who frequently interacts with different SQL databases, streamlining their workflow and enhancing productivity by reducing the need to learn multiple distinct command-line environments.

CAVEATS

usql relies on Go's database/sql drivers. While it supports a vast array of databases, the specific functionality and stability can vary depending on the underlying driver and database system. Users might need to ensure the correct drivers are available for compilation or dynamically loaded, depending on the usql distribution.

Command-line DSNs can expose sensitive credentials; prefer environment variables or configuration files for production environments. Some very specific, database-CLI-native features might not be directly replicated in usql's universal interface.

INTERACTIVE FEATURES

usql boasts intelligent auto-completion for SQL keywords, database object names (tables, columns, views), and usql's own meta-commands. It also provides syntax highlighting for queries, enhancing readability in the terminal.

META-COMMANDS

Similar to psql, usql offers a rich set of internal commands (e.g., \d, \l, \?) that allow users to inspect database schema, list connections, and manage the usql environment without writing raw SQL.

CONFIGURATION

usql supports a startup file (typically ~/.usqlrc or ~/.config/usql/usqlrc) where users can define aliases, set options, and execute commands automatically upon launching the client, enabling extensive customization.

HISTORY

usql was created by Kenneth Reitz (known for requests and pipenv) in 2017, initially envisioned as a universal client inspired by the features of dbcli/pgcli and dbcli/mycli, but written in Go. Its primary goal was to provide a consistent, feature-rich interactive SQL experience across the numerous databases supported by Go's database/sql ecosystem. Following its initial development, it has been actively maintained by a community, evolving to support more databases and features, becoming a popular choice for developers working with diverse data backends.

SEE ALSO

psql(1), mysql(1), sqlite3(1), sqlcmd(1)

Copied to clipboard