Passwords
usql takes the password of a database from the URL, or asks for it at a prompt. This page shows where a password can go, and who can read it there.
A password in a URL
Section titled “A password in a URL”A URL holds the password after the user name and a ::
usql pg://app:secret@localhost/appEvery user of the machine can read the command line of a running program, for example with ps. Your shell also writes the command to its history file. Do not give a URL with a password on the command line of a machine that other people use.
If the password holds a character that has a meaning in a URL, such as @, : or /, encode it as % and its hex code. The password a@b is a%40b in a URL:
usql pg://app:a%40b@localhost/appusql does not show a password that it knows. \conninfo and \cset show it as xxxxx. But if you type a URL with a password at the prompt of usql, for example with \c, the history file of usql keeps the line. Only you can read the history file.
The password prompt
Section titled “The password prompt”If the server refuses the login, usql prints the error and asks for the password:
$ usql pg://app@localhost/apperror: postgres: 28P01: password authentication failed for user "app"Enter password:The prompt does not show what you type. usql connects again with the password that you typed. \c asks in the same way.
usql asks only when all of these are true:
- usql reads from a terminal. In a pipe or a job with no terminal, usql prints the error and stops.
- The driver tells usql that the server refused the password. The drivers of PostgreSQL and MySQL do.
- You did not give
-w.
Ask before the first try with -W
Section titled “Ask before the first try with -W”-W (--password) makes usql ask for the password before it connects:
usql -W pg://app@localhost/app-W also works with a URL in USQL_DSN:
USQL_DSN=pg://app@localhost/app usql -W-W also works with a named connection that holds a URL:
usql -W appA named connection of a driver and its parameters holds no URL. For such a connection, -W prints error: named connection is not a url.
A URL of a database in Kubernetes holds no password, because usql reads the password from a Secret. -W prints an error for it.
Never ask with -w
Section titled “Never ask with -w”-w (--no-password) makes usql never ask. If the server refuses the login, usql prints the error and stops. Give -w to a script that runs at a terminal, so that it stops and does not wait for a password:
usql -w pg://app@localhost/app -f report.sqlThe USQL_DSN environment variable
Section titled “The USQL_DSN environment variable”When the command line names no database, usql opens the URL in the USQL_DSN environment variable. Other users cannot read the environment of your program, as they can read its command line:
USQL_DSN="pg://app:$PASSWORD@localhost/app" usqlUSQL_DSN holds a URL or the name of a connection. usql 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. \c does not read the variable.
usql has no environment variable for the password alone. The PostgreSQL driver reads PGPASSWORD, as the page of PostgreSQL says.
Named connections in config.yaml
Section titled “Named connections in config.yaml”A named connection in config.yaml keeps the password off the command line. The connection holds the password in its URL, or in the password key of its map:
connections: app: pg://app:secret@localhost/app shop: protocol: mysql username: shop password: secret hostname: localhost database: shopThen give the name in place of the URL:
usql appLeave the password out of a connection, and usql asks for it when the server refuses the login. The recording at the top of this page does that.
config.yaml is a plain text file, and usql does not check who can read it. If the file holds a password, make sure that only you can read it. On Linux:
chmod 600 ~/.config/usql/config.yamlConfigure usql says where the file is on other systems.
No password file
Section titled “No password file”usql reads no password file of its own. Earlier releases read ~/.usqlpass, and this release does not. Put each password in a named connection in config.yaml, or let usql ask for it.