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.
The query buffer
Section titled “The query buffer”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 productswhere price > 5\gA 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 |
Edit a query
Section titled “Edit a query”\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'Run a query in another way
Section titled “Run a query in another way”\g can send the result to a file, or to a command after |:
select name from products order by name \g names.txtselect 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 itemsselect count(*) as n from orders \gset orders_\echo :orders_nIf the query returns no row, or more than one row, \gset prints an error and sets no variable. Variables and conditionals explains the variables.
Run a query again and again
Section titled “Run a query again and again”\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 1To stop it, press Ctrl-C.
Run a script
Section titled “Run a script”A script is a file of SQL statements and meta commands. In usql, \i runs a script in the open session:
\i report.sqlOn the command line, -f runs a script and then exits. -c runs one command and then exits:
usql -f report.sql shop.dbusql -c 'select sum(qty) from orders' shop.dbusql also reads a script from a pipe:
echo 'select count(*) from orders' | usql shop.dbIn 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:
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:
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:
usql -X -f report.sql shop.dbErrors in a script
Section titled “Errors in a script”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 onOr turn the setting on from the command line with -U:
usql -U on_error_stop=on -f report.sql shop.dbThe exit status
Section titled “The exit status”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.
More commands
Section titled “More commands”\? lists every meta command, and so does the meta commands page. The command line options page lists the options of the command line.