PostgreSQL: Basic psql Commands

Here are some PostgreSQL commands that are similar to \dt, which is used to list all the tables in the current database:

  1. \l or \list: Lists all databases available on the PostgreSQL server.

    Example:

    \l
    
  2. \dt+: Lists all tables in the current database along with additional details like the number of rows, disk space usage, and comments on the tables.

    Example:

    \dt+
    
  3. \dn or \lS: Lists all schemas in the current database. Schemas are used to organize database objects, including tables.

    Example:

    \dn
    
  4. \di or \di+: Lists all indexes in the current database, including B-tree, GIN, GIST, and other types of indexes. The \di+ command provides additional details.

    Example:

    \di
    
  5. \dc or \dc+: Lists all privileges on tables and other objects in the current database. The \dc+ command provides additional details.

    Example:

    \dc
    
  6. \df or \df+: Lists all user-defined functions in the current database. The \df+ command provides additional details about the functions.

    Example:

    \df
    
  7. \du or \du+: Lists all database users and their roles. The \du+ command provides additional details about user roles.

    Example:

    \du
    
  8. \d+ table_name: Provides detailed information about a specific table, including column names, data types, and constraints.

    Example:

    \d+ table_name
    
  9. \d schema_name.*: Lists all tables and objects within a specific schema.

    Example:

    \d schema_name.*
    

Access or Switch a Database: \c db_name.
List All Tables: \dt.
Describe All Tables: \d.
Describe a Specific Table: \d tab_name.
List All Schemas: \dn.
List All Views: \dv.
List All Functions: \df.
List All Users: \du.
Show Commands History: \s
Save Query’s Results to a Specific File: \o file_name.
Run psql Commands/queries From a Particular File: \i file_name.
Execute Previous Command: \g.
Show Query Execution Time: \timing.
Get Output in HTML Format: \H.
Align Columns Output: \a.
Get Help: \h.
Get All psql Commands: \?.
Clear Screen: \! cls.
Quit psql: \q.

These commands are useful for exploring and managing database objects in PostgreSQL. They provide information about tables, indexes, functions, and other database-related items, allowing you to interact with the database more efficiently and effectively.

Related Articles