~/.psqlrc - Postgres Shell Tricks
psqlrc is the configuration file read by the psql client.
For full details, see the official docs and wiki:
My setup
You can find the full file here: https://github.com/k-candidate/dotfiles/blob/main/psqlrc.
Walkthrough
\set QUIET 1
Temporarily silences psql’s banner and feedback from these setup commands, so they don’t clutter the screen at startup.
\pset null '¤'
Changes how NULL values are shown in query results: instead of an empty cell, psql will print the character ¤ wherever a column is NULL.
Do not confuse NULL with ''.
\set PROMPT1 '%[%033[1m%][%/] # '
Defines the main prompt shown when psql is ready for a new command.
The %[ and %] tell psql which parts are non-printing.
The %033[1m emits the ESC (octal 033) character followed by [1m, which switches the terminal to bold.
The %/ expands to the current database name.
So, putting it all together, the prompt is a bold [dbname] # .
\set PROMPT2 '... > '
This defines the secondary prompt shown when a command is incomplete (forgetting the ; at the end). This makes it obvious that psql is still waiting for more input.
\timing
Turns on timing so that after each query I see how long it took to run.
\x auto
Enables “expanded” output automatically, switching between normal table layout and vertical (one column per line) layout to show results more clearly depending on width.
\set VERBOSITY verbose
Increases error message verbosity to have more context (e.g. which statement block failed).
\set HISTFILE ~/.psql_history- :DBNAME
Writes command history to a file whose name includes the database name (e.g. ~/.psql_history-db1), so each database has its own history file.
\set HISTCONTROL ignoredups
Tells psql’s history handling to ignore duplicate consecutive commands. This keeps the history cleaner, and makes searching quicker with the arrow keys.
\set COMP_KEYWORD_CASE upper
Makes tab-completion for SQL keywords insert them in uppercase (SELECT, FROM, etc.)
\unset QUIET
Back to normal psql output (remember the first instruction?) after all configuration commands have run.
This means that only the startup configuration is silent, but everything I do afterward behaves normally.