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.

113 lines
3.1 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
--\! echo; echo .psqlrc loaded; echo
\echo
\echo .psqlrc loaded
\echo
4 years ago
4 years ago
-- fancy colourized prompt
--
-- %m hostname -s / socket
-- %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
\set PROMPT1 '%[%033[32m%]%`date "+%T"` %[%033[35;1m%]%m:%[%033[36;1m%]%n %[%033[33;1m%]%x%[%033[;1m%]%/%[%033[0m%]%R%# '
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
-- turn pager off if inside a terminal multiplexer (screen, tmux)
\pset pager on
-- display NULLs literally instead of as blanks which is confusing
\pset null 'NULL'
-- leave wrapping to inherit \$COLUMNS
--\pset columns 80
-- enforce vertical results instead of horizontal columns (default: auto - vertical when width > columns)
--\pset expanded
-- 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
5 years ago
-- enable timings for every query
4 years ago
\timing on
5 years ago
4 years ago
-- \! echo
\echo
\conninfo
--\! echo
\echo
-- 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'