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.

155 lines
4.2 KiB
Plaintext

5 years ago
--
-- Author: Hari Sekhon
-- Date: 2020-03-16 10:14:28 +0000 (Mon, 16 Mar 2020)
--
4 years ago
-- vim:ts=4:sts=4:sw=4:et:filetype=sql
--
5 years ago
-- 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
--
4 years ago
-- See also:
--
-- https://github.com/HariSekhon/SQL-scripts
4 years ago
-- PSQL docs:
--
-- https://www.postgresql.org/docs/12/app-psql.html
4 years ago
--\! echo; echo .psqlrc loaded; echo
\echo
\echo .psqlrc loaded
\echo
4 years ago
4 years ago
---------------
-- fancy prompt
4 years ago
--
4 years ago
-- %M fqdn
-- %m hostname / local for socket
-- %> port
4 years ago
-- %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
4 years ago
------------------
-- default prompt:
--
-- db=#
--\set PROMPT1 '%/%R%# '
---------------------------
4 years ago
-- fancy prompt colorized:
-- time hostname serverhost/local user transaction database = #
4 years ago
\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%# '
4 years ago
-------------------------------------------------------------
4 years ago
-- fancy prompt non-colorized (for PostgreSQL < 9.3):
--
4 years ago
--\set PROMPT1 '%`date "+%T"` %`hostname -s` %m:%n %x>%/%R%# '
-------------------------------
4 years ago
\set PROMPT2 '[continue] %R > '
4 years ago
4 years ago
-------------------------------------------------------------------
4 years ago
-- 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'
4 years ago
-------------------------------------------------------------------
-- turn pager off if inside a terminal multiplexer (screen, tmux)
--\pset pager on
4 years ago
-- leave wrapping to inherit \$COLUMNS
--\pset columns 80
-- enforce vertical results instead of horizontal columns (default: auto - vertical when width > columns)
4 years ago
--\x auto
--\pset expanded auto
4 years ago
-- 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
4 years ago
-------------------------------------------------------------------
4 years ago
-- 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
4 years ago
\set VERBOSITY verbose
5 years ago
4 years ago
---------------------------------
5 years ago
-- enable timings for every query
4 years ago
\timing on
5 years ago
4 years ago
-- \! echo
\echo
4 years ago
---------
4 years ago
-- doesn't work on PostgreSQL <= 9.0, but doesn't error out so it's fine
4 years ago
\conninfo
--\! echo
\echo
4 years ago
-----------------------
4 years ago
-- set useful variables
-- call like this - :reload OR :r
4 years ago
-- 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"
4 years ago
\set reload '\\i ~/.psqlrc'
\set r '\\i ~/.psqlrc'
4 years ago
-- for local settings
-- XXX: see if there is a way to check for existence and source only then
--\i ~/.psqlrc.local