PostgreSQL: Top PSQL 5 Commands for 90% of the time

The following command is what I’ve been using 90% of the time.

This is specific to psql command (for PostgreSQL)

You will usually encounter when you’re

  1. Fixing something over SSH
  2. Quickly managing something in the terminal
  3. Writing the script
List all databases
\l
Change database
equivalent to SQL USE <database_name>
\c <database_name>
Bonus: select schema
\dn - list schemas
set search_path to <schema_name>
show search_path
\dt -- list tables
\dn -- list schema
\du -- list roles
Describe table
\d <table_name>
TABLE <table_name>;

This is equivalent to SELECT * FROM <table_name>;

For long input, page one-by-one with

\x auto

Non formatted dump the output

This is usually for copying purpose

\x off
\pset format wrapped
\pset pager off
do not use 'less' to view result just print out
\! clear

Bonus: getting help

\?

Hope this helps.

Cheers!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store