If you're spending time in PostgreSQL's command-line tool psql, knowing your way around meta-commands can save you time and frustration. These handy backslash (\) commands aren't SQL—they're built into psql to help you inspect, navigate, and control your PostgreSQL environment more effectively.
Meta-commands are incredibly powerful for database administrators and developers alike. They allow you to introspect your database schema, manage connections, debug queries, and automate routine operations. Since they’re executed inside the psql shell and not on the server side, they provide a lightweight and efficient way to interact with the database, especially when scripting or troubleshooting.
Unlike SQL statements, which are processed by the PostgreSQL server, meta-commands are interpreted by the psql client itself. This distinction makes them particularly useful for operations like formatting output, accessing help, and running scripts with conditional logic.
Meta-commands are what turn psql into more than just a SQL shell—they give you powerful, scriptable access to your database environment. Think of them as your DevOps swiss army knife for PostgreSQL.
This blog post is your complete guide to PostgreSQL meta-commands, categorized by their purpose so you can quickly find the right tool for the task. Whether you’re new to PostgreSQL or looking to sharpen your command-line skills, this breakdown will level up your game.
INFORMATION COMMANDS
Use these to explore and list database objects and settings:
| Command | Description |
|---|---|
| List all databases |
| List tables, views, and sequences |
| List tables only |
| List views only |
| List sequences only |
| List indexes |
| List functions |
| List schemas |
| List tablespaces |
| List conversions |
| List casts |
| Show object comments |
| List operators |
| List aggregates |
| List roles |
| List installed extensions |
| 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.
DESCRIBE COMMANDS
Drill down into the structure of your objects:
| Command | Description |
|---|---|
| Describe table structure |
| Detailed table info |
| lists materialized views |
| Detailed function info |
| Table list with size and other info |
| 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.
COMMON / UTILITY COMMANDS
Everyday tools that make psql a powerhouse:
| Command | Description |
|---|---|
| Connect to a database |
| Quit |
| Change current user password |
| Show current connection info |
| Toggle query execution time |
| Prompt for input |
| Run shell command |
| Run SQL from file |
| Redirect output to file |
| Open editor to write SQL |
| Execute query buffer |
| Auto-repeat a query every few seconds |
| Set or unset |
These commands offer an interactive edge to psql, turning it from a simple SQL shell into a smart development and administration console.
CONDITIONAL COMMANDS
Useful in scripting scenarios:
| Command | Description |
|---|---|
| Start a conditional block |
| Else-if condition |
| Else block |
| 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.
FORMATTING AND OUTPUT
Clean and customize your query results:
| Command | Description |
|---|---|
| Toggle expanded output mode |
| Unaligned output (for scripts) |
| HTML output mode |
| Control output format and options |
| 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.
HISTORY AND BUFFER COMMANDS
Handle your past and present commands:
| Command | Description |
|---|---|
| Show command history |
| Save command history to file |
| Reset query buffer |
| Separate multiple SQL commands |
| Run query result as SQL |
| Store result into |
This group of commands is especially valuable when iteratively testing or developing queries and scripts.
HELP COMMANDS
When in doubt, ask psql itself:
| Command | Description |
|---|---|
| List all |
| SQL command help (e.g., |
You don't need to remember everything. PostgreSQL's built-in help makes it easy to get instant documentation when you need it.
EXPORT / COPY DATA EASILY
Command | Description |
|---|---|
| Client-side CSV (or other format) import/export command |
Replication related meta-commands
Command | Description |
|---|---|
| List publications |
| List subscriptions |
The -E Option
When launching psql, try it with the -E option like this:
Postgres@mafireepc19:~$ psql -E -h 172.17.0.4 -p 5432 -U maf_postgres -d miru_prod
Password for user maf_postgres:
psql (16.3)
Type "help" for help.
miru_prod=# \dt
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 't' THEN 'TOAST table' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam
WHERE c.relkind IN ('r','p','')
AND n.nspname <> 'pg_catalog'
AND n.nspname !~ '^pg_toast'
AND n.nspname <> 'information_schema'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
List of relations
Schema | Name | Type | Owner
-------+-----------+-------+-------------
public | customers | table | maf_postgres
public | inventory | table | maf_postgres
public | orders | table | maf_postgres
public | products | table | maf_postgres
public | suppliers | table | maf_postgres
(5 rows)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!
Bonus Tips For Users
\x 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