Psql Tips
psql-tips.org
psql-tips.org
This can be handy if e.g. you typically run psql on the server/container running postgres itself and you want to avoid an accidentally large query from oomkilling your database. You can set FETCH_COUNT per database and per user with ALTER: https://www.postgresql.org/docs/current/config-setting.html#...
All normal settings that you can SET can also be set per-DB and per-user: https://www.postgresql.org/docs/current/config-setting.html#...
-- Show row count of last query in prompt.
-- Gosh, why did I do it like this...? There was a reason for it and it fixes
-- something, but I forgot what.
select :'PROMPT1'='%/%R%x%# ' as default_prompt \gset
\if :default_prompt
\set PROMPT1 '(%:ROW_COUNT:)%R%# '
\endif
\set QUIET \\-- Don't print welcome message etc.
\set HISTFILE ~/.cache/psql-history- :DBNAME \\-- Keep history per database
\set HISTSIZE -1 \\-- Infinite history
\set HISTCONTROL ignoredups \\-- Don't store duplicates in history
\set PROMPT2 '%R%# ' \\-- No database name in the line continuation prompt.
\set COMP_KEYWORD_CASE lower \\-- Complete keywords to lower case.
\pset linestyle unicode \\-- Nicely formatted tables.
\pset footer off \\-- Don't display "(n rows)" at the end of the table.
\pset null 'NULL' \\-- Display null values as NULL
\timing on \\-- Show query timings
\set pretty '\\pset numericlocale' \\-- Toggle between thousands separators in numbers
Storing history per-database is really useful if you regularly connect to different unrelated databases.I distinctly remember setting the prompt in such a funky way for a very specific reason; it took me a while to find a good solution. But for the life of me I can't remember why.
Set the PSQLRC environment variable to store it somewhere else (e.g. ~/.config/psqlrc).
My personal favorites: `\e` to edit your query in $EDITOR, and `psql service=my_db` to use saved connection params.
The aspect of that that's hit me a few times recently is how unusual (but great) it is that it and all other (that I've needed, anyway) pg tools use the same args for connecting to a database in the same way.
Once I realised, I renamed the `psql` wrapper script I'd written (takes an arg for the namespace name, looks up correct RDS host etc. for it) to `pgenv`, adding another argument for the real executable to run (i.e. `psql` is now `pgenv real_psql "$@"`) so it can also be used with dump, upgrade, vacuum...
I learned about them recently, it's been a massive quality of life improvement.
[1]: https://www.postgresql.org/docs/current/libpq-pgpass.html
[2]: https://www.postgresql.org/docs/current/libpq-pgservice.html
Thanks though, I didn't know about the latter, and I think I've only heard of pgpass from error messages and such, not actually looked into it before.
I've been following it for the last couple of months now and love the format! You get an email with a summary of the tip and link to the website (not that often, so it's not annoying).
\e to open new/last query in $EDITOR of your choice
\x to toggle extended display on/off (useful when a table has loads of columns and your screen runs out of width)
If you feel like stepping out of psql, pgcli is a great drop-in replacement with autocompletion/syntax highlighting and more.You can also use \gx in place of the semicolon, so you don't even need to run the query in the "wrong" mode first. Just say `SELECT * FROM wide_table \gx`.