You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.5 KiB
SQL
159 lines
4.5 KiB
SQL
--
|
|
-- Author: Hari Sekhon
|
|
-- Date: 2020-03-16 10:14:28 +0000 (Mon, 16 Mar 2020)
|
|
--
|
|
-- vim:ts=4:sts=4:sw=4:et:filetype=sql
|
|
--
|
|
-- https://github.com/harisekhon/bash-tools
|
|
--
|
|
-- License: see accompanying Hari Sekhon LICENSE file
|
|
--
|
|
-- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
|
|
--
|
|
-- https://www.linkedin.com/in/harisekhon
|
|
--
|
|
|
|
-- ========================================================================== --
|
|
-- A d v a n c e d . p s q l r c
|
|
-- ========================================================================== --
|
|
|
|
-- See also:
|
|
--
|
|
-- https://github.com/HariSekhon/SQL-scripts
|
|
|
|
|
|
-- PSQL docs:
|
|
--
|
|
-- https://www.postgresql.org/docs/12/app-psql.html
|
|
|
|
|
|
--\! echo; echo .psqlrc loaded; echo
|
|
\echo
|
|
\echo .psqlrc loaded
|
|
\echo
|
|
|
|
---------------
|
|
-- fancy prompt
|
|
--
|
|
-- %M fqdn
|
|
-- %m hostname / local for socket
|
|
-- %> port
|
|
-- %n username
|
|
-- %x transaction status - * in transaction, ! when transaction failed, ? when indeterminate
|
|
-- %/ database
|
|
-- %R = for connected, ! for disconnected, ^ for single line mode
|
|
-- %# # if superuser, > otherwise
|
|
-- %:myvar: replaced with variable myvar
|
|
-- %`command` output of shell command
|
|
|
|
------------------
|
|
-- default prompt:
|
|
--
|
|
-- db=#
|
|
--\set PROMPT1 '%/%R%# '
|
|
|
|
---------------------------
|
|
-- fancy prompt colorized:
|
|
-- time hostname serverhost/local user transaction database = #
|
|
\set PROMPT1 '%[%033[32m%]%`date "+%T"` %`hostname -s` %[%033[35;1m%]%m:%[%033[36;1m%]%n %[%033[33;1m%]%x%[%033[;1m%]>%/%[%033[0m%]%R%# '
|
|
|
|
-------------------------------------------------------------
|
|
-- fancy prompt non-colorized (for PostgreSQL 9.0 - 9.2 environments where colors aren't working - probably a term/env issue):
|
|
--
|
|
--\set PROMPT1 '%`date "+%T"` %`hostname -s` %m:%n %x>%/%R%# '
|
|
|
|
-------------------------------
|
|
\set PROMPT2 '[continue] %R > '
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- jump to your SQL scripts directory (this is done in postgres.sh)
|
|
--\cd ~/github/sql
|
|
|
|
-- separate history files per DB
|
|
\set HISTFILE ~/.psql_history- :DBNAME
|
|
|
|
-- default: 500
|
|
\set HISTSIZE 5000
|
|
|
|
-- same as in Bash, don't record duplicate commands
|
|
\set HISTCONTROL ignoredups
|
|
|
|
-- tab-complete SQL keywords to uppercase
|
|
\set COMP_KEYWORD_CASE upper
|
|
|
|
-- display NULLs literally instead of as blanks which is confusing
|
|
\pset null 'NULL'
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- turn pager off if inside a terminal multiplexer (screen, tmux)
|
|
--\pset pager on
|
|
|
|
-- leave wrapping to inherit \$COLUMNS
|
|
--\pset columns 80
|
|
|
|
-- enforce vertical results instead of horizontal columns (default: auto - vertical when width > columns)
|
|
--\x auto
|
|
--\pset expanded auto
|
|
|
|
-- replace | with \t or , to create TSV/CSV output
|
|
--\pset fieldsep
|
|
|
|
-- don't align columns for human readability (use when creating TSV/CSV output), default is 'aligned'
|
|
--\pset unaligned
|
|
|
|
-- line separator for unaligned output (default: \n)
|
|
--\pset recordsep
|
|
|
|
-- show (N row) count after each query
|
|
\pset footer on
|
|
|
|
-- show the encoding
|
|
\! printf "Encoding: "
|
|
\encoding
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- interactively disables transaction rollbacks to fix things interactively, change of default behaviour might confuse though
|
|
--\set ON_ERROR_ROLLBACK interactive
|
|
|
|
-- exit SQL script or return to interactive prompt upon first error - use this in production scripts
|
|
--\set ON_ERROR_STOP on
|
|
|
|
-- same as -s / --single-step - use this to debug SQL scripts by prompting between executing each line
|
|
--\set SINGLESTEP on
|
|
|
|
-- same as -S / --single-line - use newlines as SQL statement terminators so you don't need semicolons
|
|
--\set SINGLELINE on
|
|
|
|
-- debug SQL errors: default, verbose or terse
|
|
\set VERBOSITY verbose
|
|
|
|
---------------------------------
|
|
-- enable timings for every query
|
|
\timing on
|
|
|
|
-- \! echo
|
|
\echo
|
|
|
|
---------
|
|
-- doesn't work on PostgreSQL < 9.1, but doesn't error out so it's fine
|
|
\conninfo
|
|
|
|
--\! echo
|
|
\echo
|
|
|
|
-----------------------
|
|
-- set useful variables
|
|
-- call like this - :reload OR :r
|
|
-- or interpolate them into queries eg. SELECT * FROM :tablename;
|
|
-- single quote the value to treat as an SQL literal :'select'
|
|
-- double quote the value to treat as a SQL identifier :"tablename"
|
|
\set reload '\\i ~/.psqlrc'
|
|
\set r '\\i ~/.psqlrc'
|
|
|
|
-- for local settings
|
|
-- XXX: see if there is a way to check for existence and source only then
|
|
--\i ~/.psqlrc.local
|