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.
The forms of a URL
Section titled “The forms of a URL”driver+transport://user:pass@host/dbname?opt1=a&opt2=bdriver:/path/to/file/path/to/filename| 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 |
Short names
Section titled “Short names”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:
usql pg://postgres@localhost/postgresusql my://root@localhost/mysqlusql sq:books.dbPaths on disk
Section titled “Paths on disk”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.dbTo make a new SQLite file, give the driver, as in sq:books.db. For a new DuckDB file, give dk: or duckdb:.
A driver and its parameters
Section titled “A driver and its parameters”\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=roThis 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=appThe 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.
Defaults
Section titled “Defaults”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:
usql pg://The driver looks for the socket in the first of these directories that exists: /var/run/postgresql, /private/tmp and /tmp.
Keep a password off the command line
Section titled “Keep a password off the command line”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:
USQL_DSN="pg://app:$PASSWORD@localhost:15432/app" usqlusql 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 database in Kubernetes
Section titled “A database in Kubernetes”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.