Useful shortcuts for vi editor

Tuesday, 7 November 2023

psql cli tricks

-- show the connection info
# \conninfo

-- show IP address connected by the postgres client
# select inet_server_addr();

-- enable timing
# \timing
Timing is on.

-- enable expanded display
# \x
Expanded display is on.

-- run linux command inside psql command line
# \! date
Fri Sep  1 11:50:28 AM +03 2023

-- show the command history
# \s

-- prepare/set custom prompt:
# \set PROMPT1 '%M:%> user:%n database:%/ %l %x%# '
# \set PROMPT1 '%M:%> U:%n d:%/ %l %x%# '

PROMPT1 -> normal prompt
PROMPT2 -> if need to give more than one input command (extra prompt)
PROMPT3 -> if need to use "SQL COPY FROM STDIN" (extra prompt)

Ref: https://www.postgresql.org/docs/13/app-psql.html#APP-PSQL-PROMPTING