Skip to content

Connect to a database

usql opens a database from a URL. The command line, \connect (or \c) and \copy all take the same URLs. A URL is also called a DSN, a data source name.

Open PostgreSQL and MySQL by the names of their connections, change from one to the other with \c, and run one query with -c.
driver+transport://user:pass@host/dbname?opt1=a&opt2=b
driver:/path/to/file
/path/to/file
name
Part What it is
driver the name of the driver, or one of its aliases
transport tcp, udp, unix, or the name of a driver for ODBC and ADODB
user, pass the user name and the password
host the name of the host
dbname the database name, instance, or service name
?opt1=a&... options of the driver
/path/to/file a path on disk
name a named connection that \cset or the configuration file set

Every driver has a short name of two letters, which is usually the start of its name. For example, pg is PostgreSQL, my is MySQL, ms is SQL Server, or is Oracle and sq is SQLite:

Terminal window
usql pg://postgres@localhost/postgres
usql my://root@localhost/mysql
usql sq:books.db

If a URL has no driver, usql reads it as a path. usql opens a Unix domain socket with the MySQL driver, a directory with the PostgreSQL driver, and a file with the SQLite or DuckDB driver.

A path with no driver must exist. If it does not, usql prints an error that names the driver to use:

error: books.db does not exist: to create it, name its driver, as in sq:books.db

To make a new SQLite file, give the driver, as in sq:books.db. For a new DuckDB file, give dk: or duckdb:.

\c also takes the name of a driver, then the parameters of that driver. usql joins the parameters with spaces, and gives them to the driver as its own connection string. The URL rules of usql do not apply to them:

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

This form opens a connection string that has no URL form, such as the key=value form of PostgreSQL:

\c postgres host=localhost port=5432 user=app dbname=app

The driver is the name of a scheme or one of its aliases, such as sqlite3, sq or postgres. \cset takes the same form, as \cset NAME DRIVER PARAMS. Named connections shows it.

Most parts of a URL are optional. usql fills in what it can. This command connects to PostgreSQL as the local user, through the Unix domain socket of the local server:

Terminal window
usql pg://

The driver looks for the socket in the first of these directories that exists: /var/run/postgresql, /private/tmp and /tmp.

Every user of a machine can read the command line of a program. If the URL holds a password, give it in the USQL_DSN environment variable:

Terminal window
USQL_DSN="pg://app:$PASSWORD@localhost:15432/app" usql

usql reads USQL_DSN only when the command line names no database. It removes the variable from its own environment after it reads it, so the shell of \!, the pager and the editor do not get the password. Passwords shows the other ways to give a password.

A URL with the scheme k8s, or its short form k, opens a database that runs in Kubernetes, such as k://pg.prod/app. Databases in Kubernetes explains it.