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

tanut aran
1 min readJan 21, 2021

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

1. Navigate to the databases

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

2. Seeing stuff in the databases

\dt -- list tables
\dn -- list schema
\du -- list roles
Describe table
\d <table_name>

3. Seeing stuff in the table

TABLE <table_name>;

This is equivalent to SELECT * FROM <table_name>;

4. Good output

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

5. Clear the screen

\! clear

Bonus: getting help

\?

Hope this helps.

Cheers!

--

--