Configure usql
usql reads two files when it starts. config.yaml holds settings, print options and named connections. init.sql holds commands that usql runs before the first prompt. Both are in the configuration directory of usql.
Where the files are
Section titled “Where the files are”| System | Configuration directory |
|---|---|
| Linux and other Unix systems | $XDG_CONFIG_HOME/usql, which is ~/.config/usql when XDG_CONFIG_HOME is not set |
| macOS | ~/Library/Application Support/usql |
| Windows | %AppData%\usql |
macOS does not read XDG_CONFIG_HOME. \? variables prints the path of config.yaml on your system.
The USQL_CONFIG environment variable names another config.yaml, and the USQLRC environment variable names another init.sql. The option --config names another config.yaml for one run, and wins over USQL_CONFIG:
usql --config ./ci.yaml books.dbconfig.yaml
Section titled “config.yaml”config.yaml is a YAML file. Each section of the file is a map of names and values:
usql: prompt1: "%033[1;32m%/%033[0m%R%# "print: null: (none)connections: books: sq:books.db| Section | What it holds | The command that sets it in a session |
|---|---|---|
usql |
the settings of usql | \uset |
print |
the print options, which change the output | \pset |
connections |
the named connections | \cset |
The file has other sections too. The keys of config.yaml in the reference list each of them.
usql prints nothing when it reads the file. If usql cannot use a value, it prints an error with the file, the line and the name of the setting. Then it reads the other settings.
init.sql
Section titled “init.sql”init.sql is a usql script, as ~/.psqlrc is for psql. usql runs it after it opens the database of the command line, and before -c, -f and the first prompt. Put meta commands in it, such as \set, \pset and \uset:
\set since 1965A SQL statement in init.sql runs on the database of the command line. If you start usql with no database, the statement fails with error: not connected. If you sometimes start usql with no database, keep SQL out of init.sql.
To start usql without init.sql, give -X:
usql -X books.db-X does not skip config.yaml.
Settings
Section titled “Settings”A setting changes how usql works, such as the pager, the editor or the prompt. \uset with no argument lists every setting and its value. \uset with a name and a value changes the setting for the session:
\uset on_error_stop on\config shows and sets every kind of setting by its full name, as in usql.prompt1 or print.null:
\config print.nullusql does not save a change that \uset, \pset or \config makes. To keep a change, put it in config.yaml or in init.sql.
\? variables describes each setting. The settings of usql in the reference have the full list.
Settings on the command line
Section titled “Settings on the command line”These options set a value for one run of usql. Each takes NAME=VALUE:
| Option | What it sets | The command that sets it in a session |
|---|---|---|
-U (--uset) |
a setting of usql | \uset |
-P (--pset) |
a print option | \pset |
-N (--cset) |
a named connection | \cset |
-v (--set) |
a variable | \set |
-O (--option) |
any setting, by its full name | \config |
usql -U pager=off -P null='(null)' books.dbusql -O print.border=2 books.dbusql -N books=sq:books.db booksusql applies these options in the order that you give them. If you give the same name twice, the last value wins. If usql cannot set a value, it prints an error that names the option, and stops:
error: usql.bogus=1: unknown setting: bogususql --help lists every option.
Which value wins
Section titled “Which value wins”usql applies the values in this order. A later value wins over an earlier one:
- The defaults of usql. Some come from the environment, such as
PAGERandEDITOR. config.yaml.- The environment variables of usql, such as
USQL_PAGERandUSQL_EDITOR. - The options of the command line.
init.sql.- The commands that you type in the session.
The prompt
Section titled “The prompt”The settings prompt1 and prompt2 hold the prompt. usql shows prompt1 on the first row of a statement and prompt2 on the next rows. The default for both is %S%N%m%/%R%# .
A % and a letter in the prompt is an escape. usql puts a value in its place. These escapes make the default prompt:
| Escape | What usql shows |
|---|---|
%S |
the short name of the driver and a colon, such as pg: |
%N |
the user name and @, when the URL has a user |
%m |
the host name, up to the first dot |
%/ |
the database name |
%R |
= for a new statement, and - while a statement continues |
%# |
>, or ~ in a transaction |
%033 puts the character with the octal code 033 in the prompt. That character is Escape, which starts a color code for the terminal. This prompt shows the database name in bold green:
\uset prompt1 '%033[1;32m%/%033[0m%R%# 'The history
Section titled “The history”At a terminal, usql saves each statement and command that you type in the file history in its state directory:
| System | History file |
|---|---|
| Linux and other Unix systems | $XDG_STATE_HOME/usql/history, which is ~/.local/state/usql/history when XDG_STATE_HOME is not set |
| macOS | ~/Library/Application Support/usql/history |
| Windows | %LocalAppData%\usql\history |
The USQL_HISTORY environment variable names another file. usql does not save the history of -c and -f.
\s prints the history. \hist 10 prints the last 10 entries, with their numbers. Line editing and completion shows how to search the history.