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

Database Jul 13, 2023

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
\lChange 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 rolesDescribe 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!

Tags

TeamCMD

We are CODEMONDAY team and provide a variety of content about Business , technology, and Programming. Let's enjoy it with us.