forked from rin-nas/postgresql-patterns-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.psqlrc
56 lines (42 loc) · 1.9 KB
/
.psqlrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- From https://www.depesz.com/2021/06/23/a-tale-of-making-company-wide-standard-psqlrc/
-- Hide confirmation messages
\set QUIET
-- Check if we're connecting to database pgbouncer. It is very limited, and can't run SELECT's
\if `test :DBNAME = pgbouncer && echo 1 || echo 0`
-- This is pgbouncer. User plain prompt
\set PROMPT1 '%`date +%H:%M:%S` db: %n@%/\n%x%R%# '
\else
-- This isn't pgbouncer. So we should have SELECTs available
-- If we're connecting through pgbouncer, then prompt's %p is not real, so get real PID
SELECT pg_backend_pid() \gset
-- Check if there is dba.smi() function
select EXISTS(SELECT 1 from pg_proc p join pg_namespace n on p.pronamespace = n.oid where p.proname = 'smi' and n.nspname = 'dba') \gset smi_
\if :smi_exists
-- If dba.smi() exists, get data from it, and put it in prompt.
select
dba.smi('aws_tag_project') as project,
dba.smi('aws_tag_environment') as env,
dba.smi('aws_tag_cluster') as cluster,
case dba.smi('aws_tag_pgrole')
when 'master' then 'primary'
when 'slave' then 'secondary'
when 'backup' then 'report'
else dba.smi('aws_tag_pgrole')
end as pgrole \gset smi_
\set PROMPT1 '%`date +%H:%M:%S` %:smi_project: %:smi_env:, cluster %:smi_cluster:, %:smi_pgrole: db: %n@%/, pid:%:pg_backend_pid:\n%x%R%# '
\else
-- If dba.smi() is not available, pick simpler prompt
\set PROMPT1 '%`date +%H:%M:%S` db: %n@%/, pid:%:pg_backend_pid:\n%x%R%# '
\endif
\endif
-- Common settings, regardless of dba.smi() existence
\set PROMPT2 '%R%# '
\pset null '[null]'
-- Use sensible pager
\pset pager always
\setenv PAGER 'less -iMFXSx4R'
-- It's 21st century, let's use unicode for borders
\pset linestyle unicode
-- Stop hiding confirmation messages
\unset QUIET
-- vim: set ft=sql: