If you're searching for a complete psql commands list, practical psql commands with examples, or a quick psql commands cheat sheet, you're in the right place.
This guide is a complete reference for PostgreSQL psql meta-commands — the powerful backslash (\) commands built directly into the psql command-line interface. These commands help you inspect database objects, manage connections, format query output, debug performance issues, and automate routine database tasks — all without writing SQL.
Unlike SQL queries that run on the PostgreSQL server, psql meta-commands are executed client-side within the psql shell. This makes them extremely fast and efficient for day-to-day operations, especially when working in production environments.
At Mafiree, our PostgreSQL DBAs rely heavily on these meta-commands for quick diagnostics, performance troubleshooting, and efficient database management across large-scale production systems. Many of the commands and examples in this guide are based on real-world scenarios and best practices used by our team.
For developers and DBAs, mastering PostgreSQL psql commands is a game-changer:
These commands are widely used in real-world environments — including by database engineers at Mafiree — because they provide direct, scriptable control over PostgreSQL databases.
Use these to explore and list database objects and settings:
| Command | Description |
|---|---|
|
Databases
|
|
\lor\list |
List all databases |
|
Schema Objects
|
|
\d |
List tables, views, and sequences |
\dt |
List tables only |
\dv |
List views only |
\ds |
List sequences only |
\di |
List indexes |
\df |
List functions |
\dn |
List schemas |
\db |
List tablespaces |
|
Metadata & Types
|
|
\dc |
List conversions |
\dc |
List casts |
\dd |
Show object comments |
\do |
List operators |
\da |
List aggregates |
|
Roles & Users
|
|
\duor\dg |
List roles |
|
Extensions
|
|
\dx |
List installed extensions |
|
Configuration
|
|
\encoding |
Show current client encoding |
These are your go-to tools when you're diving into a new database and want to understand what’s inside. No need to remember complex catalog queries—these commands get you instant insight.
Drill down into the structure of your objects:
| Command | Description |
|---|---|
\d table_name |
Describe table structure |
\d+ table_name, view_name, index_NAME |
Detailed table info |
\dm |
Lists materialized views |
\df+ |
Detailed function info |
\dt+ |
Table list with size and other info |
\dp+ |
Detailed access privileges for objects |
These commands are essential when you want to understand the architecture of your database without writing complex SQL queries. They are especially handy for quick schema inspections and auditing.
Everyday tools that make psql a powerhouse:
| Command | Description |
|---|---|
\c dbname |
Connect to a database |
\q |
Quit psql |
\password |
Change current user password |
\conninfo |
Show current connection info |
\timing |
Toggle query execution time |
\prompt |
Prompt for input |
\! |
Run shell command |
\i filename |
Run SQL from file |
\o filename |
Redirect output to file |
\e |
Open editor to write SQL |
\g |
Execute query buffer |
\watch |
Auto-repeat a query every few seconds |
|
\set
/
\unset
|
Set or unset psql variables |
These commands offer an interactive edge to psql, turning it from a simple SQL shell into a smart development and administration console.
Useful in scripting scenarios:
| Command | Description |
|---|---|
\if expression |
Start a conditional block |
\elif expression |
Else-if condition |
\else |
Else block |
\endif |
End conditional block |
Conditional meta-commands make it possible to write dynamic scripts inside psql, controlling logic based on variables or environment settings. This is particularly useful for automation or configuration tasks.
Clean and customize your query results:
| Command | Description |
|---|---|
\x |
Toggle expanded output mode |
\a |
Unaligned output (for scripts) |
\H |
HTML output mode |
\pset |
Control output format and options |
\t |
Toggle tuple-only output |
When you're dealing with complex queries or large outputs, these commands help keep things readable—or exportable in the format you need.
Handle your past and present commands:
| Command | Description |
|---|---|
\s |
Show command history |
\s filename |
Save command history to file |
\r |
Reset query buffer |
\; |
Separate multiple SQL commands |
\gexec |
Run query result as SQL |
\gset |
Store result into psql variables |
This group of commands is especially valuable when iteratively testing or developing queries and scripts.
When in doubt, ask psql itself:
| Command | Description |
|---|---|
\? |
List all psql commands |
\h |
SQL command help (e.g., \h INSERT) |
You don't need to remember everything. PostgreSQL's built-in help makes it easy to get instant documentation when you need it.
| Command | Description |
|---|---|
\COPY |
Client-side CSV (or other format) import/export command |
| Command | Description |
|---|---|
\dRp |
List publications |
\dRs |
List subscriptions |
\dt+ ordersSELECT pg_size_pretty(pg_total_relation_size('orders'));\dt+\d customers\d+ orders
\copy orders TO 'orders.csv' CSV HEADER;\copy (SELECT order_id,order_date FROM public.orders) TO 'orders_report.csv' CSV HEADER;
\copy products(name, price, stock) FROM 'product.csv' CSV HEADER;
When launching psql, try it with the -E option like this:
This tells psql to display the underlying SQL queries that it uses for meta-commands. It’s a hidden gem for anyone wanting to learn PostgreSQL internals or debug what’s really happening under the hood. For instance, running \dt will not only show tables but also the exact SQL behind that listing.
Use -E to educate yourself while working—turning every command into a learning opportunity!
\gx with a SELECT to instantly improve readability.\timing to benchmark queries during optimization.\! to integrate with your OS tools.\i.psql into a scripting-friendly powerhouse.Whether you're a PostgreSQL beginner or a power user, mastering these meta-commands can dramatically improve your productivity in psql. Bookmark this page or print it out—it's your ultimate cheat sheet for navigating PostgreSQL like a pro.
With consistent usage, you'll find yourself depending less on external documentation and more on your command-line reflexes. The real strength of PostgreSQL isn't just in its powerful SQL engine, but also in the richness and depth of tools like psql. Embrace the backslash and make your workflow smoother, faster, and smarter.
? Ready to take your psql skills to the next level? Start incorporating meta-commands into your daily database tasks and feel the difference.
Need help with your database?
Whether it’s performance tuning, audits, monitoring, high availability, or solving critical PostgreSQL issues—we’ve got you covered.
Feel free to reach out to us at sales@mafiree.com for expert guidance and support.
Miru IT Park, Vallankumaranvillai,
Nagercoil, Tamilnadu - 629 002.
Unit 303, Vanguard Rise,
5th Main, Konena Agrahara,
Old Airport Road, Bangalore - 560 017.
Call: +91 6383016411
Email: sales@mafiree.com