PostgreSQL Shortcut Commands
PostgreSQL provides a powerful command-line tool called `psql` to manage and interact with your databases. This page outlines essential `psql` shortcut commands to help you efficiently manage databases, roles, schemas, and objects.
General Shortcuts
Command | Description |
\? | Lists all `psql` commands and their descriptions. |
\q | Exits the `psql` client. |
\c [dbname] | Connects to a specific database. |
\conninfo | Displays connection details of the current session. |
\encoding | Shows or sets the client character encoding. |
\! [command] | Executes a shell command. |
\pwd | Displays the current working directory. |
Database Management
Command | Description |
\l | Lists all databases. |
\l+ | Lists all databases with additional details (size, owner, etc.). |
\g | Executes the previous SQL command. |
\i [file] | Executes SQL commands from a file. |
\watch [interval] | Re-runs the last query at specified intervals (seconds). |
Schemas and Objects
Command | Description |
\dn | Lists all schemas in the current database. |
\dn+ | Lists schemas with additional details. |
\dt | Lists all tables in the current schema. |
\dt+ | Lists tables with additional details (e.g., size, storage). |
\dv | Lists all views in the current schema. |
\dv+ | Lists views with additional details. |
\ds | Lists all sequences in the current schema. |
\ds+ | Lists sequences with additional details. |
\df | Lists all functions in the current schema. |
\df+ | Lists functions with argument and return type details. |
\db | Lists all tablespaces in the database cluster. |
\db+ | Lists tablespaces with more details. |
Object Descriptions
Command | Description |
\d [object] | Describes a specific object (table, view, etc.). |
\d+ [object] | Provides detailed information about an object. |
\d | General object listing in the current schema. |
\z | Lists access privileges for all objects. |
\dp | Lists access privileges for tables, views, and sequences. |
Role and User Management
Command | Description |
\du | Lists all roles (users) in the database cluster. |
\du+ | Lists roles with additional details. |
Query Output
Command | Description |
\x | Toggles expanded output mode (vertical format). |
\o [file] | Redirects query output to a file. |
\o | Resets output redirection to display in the terminal. |
\t | Toggles display of table headings and row counts. |
\a | Toggles between aligned and unaligned output. |
\pset | Sets output formatting options (e.g., border, null values). |
\timing | Toggles query execution time display. |
Recent Posts