Usql offers a single user interface for managing multiple database systems
Connecting to Databases
Usql opens a database connection by parsing a string and passing its content to the appropriate database driver. Database connection strings (aka "data source names" or DSNs) can be passed to usql directly on the command line or at any moment during an interactive session. In an interactive session, you can pass all the necessary parameters (driver, database name, etc.) separately via the \c
meta command. In general, DSNs that connect to multi-user database servers like PostgreSQL or MariaDB have the following structure:
driver+transport://user:pass@host/dbname
The driver
part is the name of the driver you wish to use, which corresponds to the type of database, or any of its aliases allowed by usql. When connecting to a PostgreSQL database, for example, the driver may be postgres
or pg
, whereas with MySQL, you should use mysql
or just my
. The user
, pass
(that is, password
), and dbname
components are self-explanatory, and the host
is the name or IP address of the computer hosting the database. If that computer is the same as the computer where you run usql, host
should be the same as localhost
.
The transport
part is not mandatory, and unless you work with a database that requires it, or with non-default connection protocols, you may never need it.
To connect to a database from the Linux command line, use the following command:
#> usql my://marco:mypassword@localhost/customers
or, after launching usql, from its own prompt:
\c my://marco:mypassword@localhost/customers
The steps are slightly different if you want to connect to a serverless, file-based database like SQLite 3. In this case, the DSN has a much simpler structure:
driver:/path/to/file-on-disk
where the driver could be sqlite3
, sq
, or file
, or the command might not specify a driver. Either of the following commands would open an already existing SQLite3 database contained in the single file $HOME/my-sqlite-db.sqlite3
:
#> usql sqlite3://$HOME/my-sqlite-db.sqlite3 #> usql $HOME/my-sqlite-db.sqlite3
The second command will work without errors only if the $HOME/my-sqlite-db.sqlite3
file already exists and is, indeed, an SQLite3 database. If your intention is to make usql create a new, empty SQLite3 database and save it into a file with the specified name, you must use the first command, which includes a recognizable driver name. In this case, be sure to use the right number of slashes: the sq:
and sqlite3:
driver names want two slashes after the colon, whereas the file:
name only requires one:
#> usql sqlite3://$HOME/my-sqlite-db.sqlite3 #> usql file:/$HOME/my-sqlite-db.sqlite3
To close an open connection from inside usql, just type \Z
. In any moment, you can also verify the parameters of the connection you are using by typing \conninfo
.
Editing and Reusing Queries
Once you are connected to a database from inside a usql session, you can communicate with it just as if you were using a native client. Usql also keeps the current query in a dedicated buffer that you can edit and reuse. Use the \e
meta command to edit the buffer and the \w
command to write a query into the buffer. Optional arguments for a file name and line number allow you to edit queries saved to disk in previous sessions. The \p
command shows the buffer contents and \r
resets the buffer.
Use the \g
command to re-execute the query in the buffer. If you also want to execute every value of the result, type \gexec
. The \gset
command stores the result of the query in usql variables for further reuse.
Output Formatting
A nice feature of usql is its many options for how to print the result of a query. You can print in CSV, JSON, or table format, with either plain text or HTML. Print to CSV or JSON with the -C
and -J
options. The options for printing to tables are too many to describe, but they are mostly self-explanatory. \H
toggles the HTML output mode and \T
, followed by a string of valid HTML table attributes, will apply the attributes to the current table.
« Previous 1 2 3 Next »
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.