A universal command-line interface for PostgreSQL, MySQL, Oracle Database, SQLite3, Microsoft SQL Server,and many other databasesincluding NoSQL and non-relational databases!
InstallingBuildingUsingDatabase SupportFeatures and CompatibilityReleases
Overview
USQL
provides a simple way to work withSQL and NoSQL databasesvia a command-line inspired by PostgreSQL’sPSQL
.USQL
supports most of the corePSQL
features, such as (variables) ,Backticks, andcommandsand has additional features thatPSQL
does not, such assyntax highlighting, context-based completion, andmultiple database support.
Database administrators and developers that would prefer to work with a tool likePSQL
with non-PostgreSQL databases, will findUSQL
intuitive, easy-to-use, and a great replacement for the command-line clients / tools for other databases.
Installing
USQL (can be installed) via Release,via Homebrew,via Scooporvia Go:
Installing via Release
- Download a release for your platform
- Extract the
USQL
orusql.exe
file from the.tar.bz2
or.zip
file - Move the extracted executable to somewhere on your
$ PATH
(Linux / macOS) or% PATH%
(Windows)
Installing via Homebrew (macOS)
USQL (is available in the
XO / XO
tap, and can be installed in the usual way with theBREW
command:
# (add tap) $ brew tap xo / xo#install usql with "most" drivers$ brew install usql
Additional support forOracle and ODBC databasescan be installed by passing- with - *
parameters during install:
#install usql with oracle and odbc support$ brew install --with-oracle --with-odbc usql
Please note that Oracle Database support requires using the(XO / XO) tap’sinstantclient-sdk
formula. Any otherinstantclient-sdk
formulae or older versions of the Oracle Instant Client SDKshould be uninstalledprior to attempting the above:
#uninstall the instantclient- sdk formula$ brew uninstall InstantClientTap / instantclient / instantclient-sdk#remove conflicting tap$ brew untap InstantClientTap / instantclient
Installing via Scoop (Windows )
USQL
can be installed usingScoop:
#install scoop if not already installediex (new-objectnet.webclient) .downloadstring (''https://get.scoop.sh'') scoop install usql
Installing via Go
USQL
can be installed in the usual Go fashion:
#install usql with basic database support (includes PosgreSQL, MySQL, SQLite3, and MS SQL drivers)$ go get -u github.com/xo/usql
Support for additional databases can be specified withbuild tags:
#install usql with most drivers (excludes drivers requiring CGO)$ go get -u -tags most github.com/xo/usql#install usql with all drivers (includes drivers requiring CGO, namely Oracle and ODBC drivers)$ go get -u -tags all github.com/xo/usql
Building
When building (USQL) with Go, only drivers for PostgreSQL, MySQL, SQLite3 and Microsoft SQL Server will be enabled by default. Other databases can be enabled by specifying the build tag for theirdatabase driver. Additionally, themost
andall
build tags include most, and all SQL drivers, respectively:
# (install all drivers) $ go get -u -tags all github.com/xo/usql#install with most drivers (same as all but excludes Oracle / ODBC)$ go get -u -tags most github.com/xo/usql#install with base drivers and Oracle / ODBC support$ go get -u -tags'ORACLE ODBC''github.com/xo/usql
For every build tag
, there is also the (no _) build tag disabling the driver:
#install all drivers excluding avatica and couchbase$ go get -u -tags'all no_avatica no_couchbase''github.com/xo/usql
Database Support
USQL
works with all Go standard library compatible SQL drivers supported bygithub .com / XO / Dburl
.
The list of drivers thatUSQL
was built with can be displayed using the drivers
command:
$ (CD) ($ GOPATH) / src / github.com / xo / usql $ go build -tags'no_most postgres mysql cql sqlite3''&&./usql Type"help"forhelp. (not connected)=> drivers Available Drivers: cockroachdb (postgres) [cr, cdb, crdb, cockroach] memsql (mysql) [me] mssql mysql [my, maria, aurora, mariadb, percona] postgres redshift (postgres) sqlite3 tidb (mysql) [ti] vitess (mysql) [vt] (not connected)=>
The above shows that (USQL) was built with only thepostgres
,mysql
, (CQL) , andsqlite3
Drivers. The output above reflect information about the drivers availabl e toUSQL
, specifically the available driver and its primary URL scheme, the driver’s available aliases (shown in[...]
), and the real / underlying driver (shown in(...)
) for the database.
Any of the protocol schemes or aliases shown above can be used in conjunction with the connect
commandwhen connecting to a database.
Supported Database Schemes and Aliases
The following is a table of all drivers, schemes, and aliases thatUSQL
supports:
Microsoft SQL Server (mssql) | ms, sqlserver |
MySQL (mysql) | my, mariadb, maria, percona, aurora |
Oracle Database (goracle) | or, oracle, oci8, oci, odpi, odpi-c |
PostgreSQL (postgres) | pg, postgresql, pgsql |
SQLite3 (sqlite3) | sq, sqlite, file |
Amazon Redshift (redshift) | RS [postgres] |
CockroachDB (cockroachdb) | cr, cockroach, crdb, cdb [postgres] |
MemSQL (memsql) | me [mysql] |
TiDB (tidb) | Ti [mysql] |
Vitess (vitess) | VT [mysql] |
Google Spanner (spanner) | gs, google, span (not yet public) |
MySQL (mymysql) | ZM, MYMY |
PostgreSQL (pgx) | px |
Apache Avatica (avatica) | av, phoenix |
Apache Ignite (ignite) | ig, gridgain |
Cassandra (cql) | ca, cassandra, datastax, scy, scylla |
ClickHouse (clickhouse) | ch |
Couchbase (n1ql) | N1, Couchbase |
Cznic QL (ql) | ql, cznic, cznicql |
Firebird SQL (firebirdsql) | FB, firebird |
Microsoft ADODB (adodb) | ad, ado |
ODBC (odbc) | OD |
OLE ODBC (oleodbc) | oo, ole, oleodbc [adodb] |
Presto (presto) | pr, prestodb, prestos, prs, prestodbs |
SAP ASE (tds) | ax, ase, sapase |
SAP HANA (hdb) | sa, saphana, sap, hana |
Snowflake (snowflake) | SF |
VoltDB (voltdb) | vo, volt, vdb |
Go Drivers and Build Tags
The following are theGo SQL drivers (that) USQLsupports, and the associated Go build tag:
Using
Afterinstalling,USQL
can be used similarly to the following:
#connect to a postgres database$ usql postgres: // booktest @ localhost / booktest#connect to an oracle database$ usql oracle: // user: [email protected]/oracle.sid#connect to a postgres database and run script.sql$ usql pg: // localhost / -f script.sql
Command-line Options
Supported command-line options:
$ usql --help usql, the universal command-line interfaceforSQL databases Usage: USQL [OPTIONS] ... [DSN] Arguments: DSN database url Options: -c, --command=COMMAND ... run only singlecommand(SQL or internal) andexit -f, --file=FILE ... execute commands from file and (exit) -w, --no-password never promptforPassword -X, --no-rcDOnotreadstart up file -o, --out=OUT output file -W, --password force password prompt (should happen automatically) -1, --single-transaction execute as a single transaction (if non-interactive) -v, --set=, --variable=NAME=VALUE ... setvariable NAME to VALUE -P, --pset=VAR [=ARG] ...setprinting option VAR to ARG (see pset command) -A, --no-align unaligned table output mode -F, --field-separator=TEXT field separatorforunaligned output (default,"|") -H, --html HTML table output mode -R, --record-separator=TEXT record separatorforunaligned output (default, n) -t, --tuples-only print rows only -T, --table-attr=TEXTsetHTML table tag attributes (eg, width, border) -x, --expanded turn on expanded table output -z, --field-separator-zerosetfield separatorforunaligned output to zero byte -0, --record-separator-zerosetrecord separatorforunaligned output to zero byte -J, --json JSON output mode -C, --csv CSV output mode -V, --version display version andexit
Connecting to Databases
USQL (opens a database connection byparsing a URLand passing the resulting connection string toa database driver. Database connection strings (aka "data source name" or DSNs) have the same parsing rules as URLs, and can be passed to
USQL
via command-line, or to the connect
or c
commands.
Connection strings look like the following:
driver transport: // user: pass @ host / dbname? opt1=a & opt2=b driver: / path / to / file / path / to / file
Where the above are:
driver name or alias | |
transport | TCP ,UDP ,UNIX or driver name(for ODBC and ADODB) |
user | username |
pass | password |
host | hostname |
dbname* | database name, instance, or service name / ID |
? opt1=a & … | additional database driver options (see respective SQL driver for available options) |
/ path / to / file | a path on disk |
*for Microsoft SQL Server,/ dbname
can be/ instance / dbname
, where/ instance
is optional. For Oracle Database,/ dbname
is of the form/ service / dbname
where/ service
is the service name or SID, and/ dbname
is optional. Please see below for examples.
Driver Aliases
USQL
supports the same driver names and aliases from thedburl
package. Most databases have at least one or more alias – please refer to thedburl
documentationfor all supported aliases.
Short Aliases
All database drivers have a two character short form that is usually the first two letters of the database driver. For example,PG
forpostgres
,my
formysql
, (ms) formssql
,or (for) oracle
, or (sq) forsqlite3
.
Features and Compatibility
The (USQL) project’s goal is to support all standardPSQL
commands and features. Pull Requests are always appreciated!
Variables and Interpolation
USQL
supports client-side interpolation of variables that can be set
and unset
:
$ usql (not connected)=> set (not connected)=> set FOO bar (not connected)=> set FOO='bar'(not connected)=> unset FOO (not connected)=> set (not connected)=>
A set
variable,NAME
, will be directly interpolated (by string substitution) into the query when prefixed with:
and optionally surrounded by quotation marks ('
or"
):
pg: booktest @ localhost=> set FOO bar pg: booktest @ localhost=>select*from authors where name=:'' (FOO) ''; author_id|name ----------- ------ 7|bar (1 rows)
The three forms,: NAME
,: 'NAME'
, and: "NAME"
, are used to interpolate a variable in parts of a query that may require quoting, such as for a column name, or when doing concatenation in a query:
pg: booktest @ localhost=> set TBLNAME authors pg: booktest @ localhost=> set COLNAME name pg: booktest @ localhost=> set FOO bar pg: booktest @ localhost=>select*from: TBLNAME where:"COLNAME"(=:'FOO'pg: booktest @ localhost -> pselect*from authors where"" (name) "=' (bar) 'pg: booktest @ localhost -> rawselect*from: TBLNAME where:""COLNAME"=:''FOO''pg: booktest @ localhost -> g author_id|name ----------- ------ 7|bar (1 rows) pg: booktest @ localhost=>
(Note) : variables contained within other strings willNOTbe Interpolated:
pg: booktest @ localhost=>select': FOO'; ?column? ---------- : FOO (1 rows) pg: booktest @ localhost=> pselect': FOO';pg: booktest @ localhost=>
Backtick’d parameters
(Meta)) commandssupport backticks on parameters:
(not connected)=> echo Welcome`` (echo) $ USER(``-''Currently:''"(""`` (date) `")""Welcome ken - currently: (Wed Jun 13 12: 10: 27 WIB 2018) (not connected)=>
Backtick’d parameters will be passed to the user’sSHELL
, exactly as written, and can be combined with set
:
pg: booktest @ localhost=> set MYVAR`` (date) ``pg: booktest @ localhost=> set MYVAR=' (Wed Jun) 12: 17: 11 WIB 2018'pg: booktest @ localhost=> echo: MYVAR Wed Jun 13 12: 17: 11 WIB 2018 pg: booktest @ localhost=>
Passwords
USQL
supports reading passwords for databases from a.usqlpass
file contained in the user’s (HOME) directory at startup:
$ cat$ HOME/ .usqlpass#format is :#protocol: host: port: dbname: user: passPostgres:*:*:*: booktest: booktest $ usql pg: // Connected with driver postgres (PostgreSQL 9.6.9) Type"help"forhelp. pg: booktest @=>
Note: the.usqlpass
file cannot be readable by other users. Please set the permissions accordingly:
Runtime Configuration (RC) File
USQL
supports executing a.usqlrc
contained in the user’s (HOME) directory:
$ cat$ HOME/ .usqlrc echo WELCOME TO THE JUNGLE`` (date) `` set SYNTAX_HL_STYLE paraiso-dark $ usql WELCOME TO THE JUNGLE Thu Jun 14 02: 36: (WIB) Type"help"forhelp. (not connected)=> set SYNTAX_HL_STYLE='paraiso-dark''(not connected)=>
The.USQLC
file is read byUSQL
at startup in the same way as a file passed on the command-line with- f
/- file
. It is commonly used to set startup environment variables and settings.
You can temporarily disable the RC-file by passing- X
or- no-rc
on the command-line:
Host Connection Information
By default, (USQL) displays connection information when connecting to a database. This might cause problems with some databases or connections. This can be disabled by setting the system environment variableUSQL_SHOW_HOST_INFORMATION
tofalse
:
$exportUSQL_SHOW_HOST_INFORMATION=false $ usql pg: // booktest @ localhost Type"help"forhelp. pg: booktest @=>
SHOW_HOST_INFORMATION (is a standard)
USQL (variable) , and can be
set
or unset
. Additionally, it can be passed via the command-line using- V
or- set
:
$ usql --set SHOW_HOST_INFORMATION=false pg: // Type"help"forhelp. pg: booktest @=> set SHOW_HOST_INFORMATION (true) pg: booktest @=> connect pg: // Connected with driver postgres (PostgreSQL 9.6.9) pg: booktest @=>
Syntax Highlighting
Interactive queries will be syntax highlighted by default, using Chroma. There are a number ofvariablesthat control syntax highlighting:
(SYNTAX_HL) | (true) | (true) or (false) | enables syntax highlighting |
SYNTAX_HL_FORMAT |
dependent on terminal support | formatter name | (Chroma formatter name) |
SYNTAX_HL_OVERRIDE_BG |
(true) | (true) or (false) | enables overriding the background color of the chroma styles |
SYNTAX_HL_STYLE |
Monokai |
style name | Chroma style name |
Time Formatting
Some databases support time / date columns thatsupport formatting. By default,USQL
formats time / date columns as (RFC) Nano, and can be set using the(TIME_FORMAT) ****************************************************** (variable) :
$ usql pg: // Connected with driver postgres (PostgreSQL 9.6.9) Type"help"forhelp. pg: booktest @=> set TIME_FORMAT=' (RFC) *********************************************************************************************************************************************************************** (Nano) ''pg: booktest @=>select(now (); now ---------------------------------- 2018 - 06 - (T) : 24: 12. 481923 07: 00 (1 rows) pg: booktest @=> set TIME_FORMAT Kitchen pg: booktest @=> g now -------- 3: 24 AM (1 rows)
AnyGo supported time formator const name (for example,Kitchen
, in the above) can be used for (TIME_FORMAT) .
TODO
USQL
aims to eventually provide a drop-in replacement for PostgreSQL’s (PSQL) command. This is on-going – an attempt has been made in good-faith to provide support for the most frequently used aspects / features of (PSQL) . Compatability (where possible) withPSQL
, takes general development priority.
GIPHY App Key not set. Please check settings