Skip to content

Named connections

A named connection is a short name for the URL of a database. You type the name in place of the URL, on the command line, with \c and with \copy. A name set with \cset lasts until usql exits. A name in config.yaml is there each time that usql starts.

Open a SQLite file by a name from config.yaml, name a DuckDB file with \cset, and change between the two.

\cset takes a name and a URL:

\cset shelf duckdb:shelf.duckdb

\c opens the connection by its name:

\c shelf

\cset with no argument lists the names and their URLs. It shows a password in a URL as xxxxx:

sq:books.db=> \cset
books 'sq:books.db'
shelf 'duckdb:shelf.duckdb'

\cset with only a name removes that name. usql does not save a name that \cset sets.

The connections: section of config.yaml holds the names that usql knows when it starts. Configure usql says where the file is on your system.

connections:
books: sq:books.db
app:
protocol: postgres
username: app
hostname: localhost
port: 5432
database: app

A name has a URL, as books has, or a map of the parts of a URL, as app has. usql makes the URL postgres://app@localhost:5432/app from the map of app.

Give the name on the command line in place of the URL:

Terminal window
usql books

config.yaml is a plain text file. If a URL in it holds a password, make sure that only you can read the file. On Linux:

Terminal window
chmod 600 ~/.config/usql/config.yaml

On another system, use the path that \? variables prints.

A name can also hold the name of a driver and its parameters, as \c takes them. \cset takes them after the name:

\cset ro sqlite3 file:shop.db?mode=ro

In config.yaml, write them as a list:

connections:
ro: [sqlite3, "file:shop.db?mode=ro"]

Such a name holds no URL, so -W does not work with it. Passwords explains -W.

\copy takes a name in place of each URL:

\cset shop sq:shop.db
\cset warehouse dk:warehouse.duckdb
\copy shop warehouse 'select id, name, price from products' products

Copy data between databases explains \copy.

usql looks for a name before it reads the argument as a path. If a file in the current directory has the same name as a connection, usql opens the connection. To open the file, give a path such as ./books.