Skip to content

Run queries and scripts

This page explains how usql collects a statement, the meta commands that run it, and how usql runs a script. A meta command is a command that starts with a backslash, such as \g. usql runs a meta command itself, and does not send it to the database.

Run a query on two lines, print the query buffer, show a row in expanded mode, keep a result in variables, run a query each second, and run one query from the shell.

usql keeps the statement that you type in the query buffer. A statement can span lines. While the buffer holds text, the prompt ends with -> in place of =>.

usql runs the statement when a line ends with a semicolon. It also runs it when you type \g:

select name, price from products
where price > 5
\g

A semicolon inside a string, a quoted name or a comment does not end the statement.

These meta commands work on the query buffer:

Command What it does
\p prints the query buffer, or the last query that ran
\r clears the query buffer
\e opens the query buffer, or the last query that ran, in an editor

\e writes the query to a temporary file and opens the file in your editor. When you close the editor, usql reads the file again. If the text ends with a semicolon, usql runs it. If it does not, the text stays in the query buffer.

usql uses the editor setting. Its default comes from the EDITOR or the VISUAL environment variable. If neither is set, usql uses vi. To use another editor, set it with \uset:

\uset editor 'code --wait'

\g can send the result to a file, or to a command after |:

select name from products order by name \g names.txt
select name from products \g |wc -l

\gx runs the query and prints the result in expanded mode. Expanded mode prints each row as a list of its columns, one column on each line. It helps when a row is too wide for the terminal:

select * from products where id = 2 \gx

\gset runs a query that returns one row. It keeps each column in a variable with the name of the column. Add a prefix after \gset to put it before each name:

select count(*) as n, sum(qty) as items from orders \gset
\echo :n orders, :items items
select count(*) as n from orders \gset orders_
\echo :orders_n

If the query returns no row, or more than one row, \gset prints an error and sets no variable. Variables and conditionals explains the variables.

\watch runs the query again and again, and prints the time before each result. Give the interval in seconds. The default is 2 seconds:

select count(*) as orders from orders \watch 1

To stop it, press Ctrl-C.

A script is a file of SQL statements and meta commands. In usql, \i runs a script in the open session:

\i report.sql

On the command line, -f runs a script and then exits. -c runs one command and then exits:

Terminal window
usql -f report.sql shop.db
usql -c 'select sum(qty) from orders' shop.db

usql also reads a script from a pipe:

Terminal window
echo 'select count(*) from orders' | usql shop.db

In a script, the last statement runs even when it has no semicolon.

You can give -c and -f more than once. usql runs them in the order that you give them.

-v sets a variable before the script runs, as \set does. The script reads it as :name:

Terminal window
usql -v id=2 -c 'select name from products where id = :id' shop.db

-o sends the results to a file in place of the terminal, as \o does. \echo still writes to the terminal:

Terminal window
usql -o names.txt -c 'select name from products' shop.db

-1 runs all of the -c and -f in one transaction. Transactions explains it.

usql runs the startup script init.sql before a script, if the file exists. To start usql without it, add -X:

Terminal window
usql -X -f report.sql shop.db

If a statement fails, usql prints the error and runs the rest of the script. To stop the script at the first error, put this line at the top of the script:

\uset on_error_stop on

Or turn the setting on from the command line with -U:

Terminal window
usql -U on_error_stop=on -f report.sql shop.db

usql exits with a status that a shell or a CI job can test, as psql does:

Status When
0 usql ran everything. A statement that fails in a file of -f or in a pipe does not change the status
1 a -c failed, or usql did not connect or did not read a file
3 on_error_stop stopped a script at an error

A -c fails when one of its statements or meta commands fails. The next -c or -f still runs, unless on_error_stop is on. To make a script fail on its first error, turn on on_error_stop.

\? lists every meta command, and so does the meta commands page. The command line options page lists the options of the command line.