Mafiree logo
  • About
  • Services
  • Blogs
  • Careers
  • Products
    • orbit logo Orbit
    • streamer logo Xstreami
  • Contact
Schedule a Call
Menu
  • About
  • Services
  • Blogs
  • Careers
  • Products
    • orbit logo Orbit
    • streamer logo Xstreami
  • Contact
  • Schedule a Call
Database
Database Database Managed Services
MySQL MySQL
MySQL Consulting Services
MySQL Migration Services
MySQL Optimization & Query Tuning
MySQL Database Administration
MySQL Backup & Recovery
MySQL Security & Maintenance
MySQL Cloud Services (AWS RDS, Aurora, Google Cloud SQL, Azure)
MySQL for Ecommerce
MySQL High Availability & Replication
MongoDB MongoDB
MongoDB Consulting Services
MongoDB Migration Services
MongoDB Optimization & Query Tuning
MongoDB Database Administration
MongoDB Backup & Recovery
MongoDB Security & Maintenance
MongoDB Cloud (Atlas)
MongoDB Solutions by Industry
MongoDB High Availability & Replication
PostgreSQL PostgreSQL
PostgreSQL Consulting
PostgreSQL Migration & Upgrades
Performance Tuning & Query Optimization
PostgreSQL Administration & Managed Services
High Availability, Clustering & Replication
PostgreSQL Backup, Recovery & Disaster Planning
PostgreSQL Security, Compliance & Auditing
PostgreSQL for Analytics & Data Warehousing
PostgreSQL on Cloud & Containers
PostgreSQL Extensions & Open-Source Integrations
PostgreSQL for Every Industry
SQL Server MSSQL
MSSQL Consulting Services
MSSQL Migration Services
MSSQL Optimization & Query Tuning Services
MSSQL Database Administration Services
MSSQL Backup & Recovery Services
MSSQL High Availability & Replication Services
MSSQL Security & Compliance Services
MSSQL Performance Monitoring & Health Checks
MSSQL Solutions by Industry
Aerospike Aerospike
Aerospike Consulting Services
Aerospike Migration Services
Aerospike Performance Optimization & Tuning
Aerospike Database Administration
Aerospike Backup & Recovery
Aerospike High Availability
Aerospike Cloud & Hybrid Deployments
Aerospike for Real-Time Applications (AdTech, FinTech, Retail, IoT)
Analytics DB
Analytics DB Analytics DB Services
Clickhouse Clickhouse
ClickHouse Consulting Services
ClickHouse Migration Services
ClickHouse Optimization & Query Tuning
ClickHouse Database Administration
ClickHouse Backup & Recovery
ClickHouse Security & Maintenance
ClickHouse Cloud Services (ClickHouse Cloud, AWS, GCP, Azure)
ClickHouse Solutions by Industry
ClickHouse High Availability & Replication
TiDB TiDB
TiDB Consulting & Architecture Planning
TiDB Administration & Maintenance
TiDB Security and Privacy Maintenance
TiDB Performance & Query Optimization
TiDB Migration Services
TiDB Backup & Disaster Recovery
TiDB High Availability Solutions
TiDB Solutions by Industry
TiDB Cloud Services
ScyllaDB ScyllaDB
ScyllaDB Consulting & Architecture Planning
ScyllaDB Administration & Maintenance
ScyllaDB Security and Privacy Maintenance
ScyllaDB Performance & Query Optimization
ScyllaDB Migration Services
ScyllaDB Backup & Disaster Recovery
ScyllaDB High Availability Solutions
ScyllaDB Solutions by Industry
ScyllaDB Cloud Services
DevOps
DevOps DevOps Services
Version Control Version Control
Kubernetes Kubernetes
Infrastructure Infrastructure Management
Web Servers Web Servers
Networking
Networking Networking Services
Basic Basic
Advanced Advanced
MySQL MySQL
MongoDB MongoDB
PostgreSQL PostgreSQL
MSSQL MSSQL
Aerospike Aerospike
Clickhouse Clickhouse
TiDB TiDB
ScyllaDB ScyllaDB
Version Control Version Control
Kubernetes Kubernetes
Infrastructure Infrastructure Management
Web Servers Web Servers
Basic Basic
Advanced Advanced
MySQL Consulting Services
MySQL Migration Services
MySQL Optimization & Query Tuning
MySQL Database Administration
MySQL Backup & Recovery
MySQL Security & Maintenance
MySQL Cloud Services (AWS RDS, Aurora, Google Cloud SQL, Azure)
MySQL for Ecommerce
MySQL High Availability & Replication
MongoDB Consulting Services
MongoDB Migration Services
MongoDB Optimization & Query Tuning
MongoDB Database Administration
MongoDB Backup & Recovery
MongoDB Security & Maintenance
MongoDB Cloud (Atlas)
MongoDB Solutions by Industry
MongoDB High Availability & Replication
PostgreSQL Consulting
PostgreSQL Migration & Upgrades
Performance Tuning & Query Optimization
PostgreSQL Administration & Managed Services
High Availability, Clustering & Replication
PostgreSQL Backup, Recovery & Disaster Planning
PostgreSQL Security, Compliance & Auditing
PostgreSQL for Analytics & Data Warehousing
PostgreSQL on Cloud & Containers
PostgreSQL Extensions & Open-Source Integrations
PostgreSQL for Every Industry
MSSQL Consulting Services
MSSQL Migration Services
MSSQL Optimization & Query Tuning Services
MSSQL Database Administration Services
MSSQL Backup & Recovery Services
MSSQL High Availability & Replication Services
MSSQL Security & Compliance Services
MSSQL Performance Monitoring & Health Checks
MSSQL Solutions by Industry
Aerospike Consulting Services
Aerospike Migration Services
Aerospike Performance Optimization & Tuning
Aerospike Database Administration
Aerospike Backup & Recovery
Aerospike High Availability
Aerospike Cloud & Hybrid Deployments
Aerospike for Real-Time Applications (AdTech, FinTech, Retail, IoT)
ClickHouse Consulting Services
ClickHouse Migration Services
ClickHouse Optimization & Query Tuning
ClickHouse Database Administration
ClickHouse Backup & Recovery
ClickHouse Security & Maintenance
ClickHouse Cloud Services (ClickHouse Cloud, AWS, GCP, Azure)
ClickHouse Solutions by Industry
ClickHouse High Availability & Replication
TiDB Consulting & Architecture Planning
TiDB Administration & Maintenance
TiDB Security and Privacy Maintenance
TiDB Performance & Query Optimization
TiDB Migration Services
TiDB Backup & Disaster Recovery
TiDB High Availability Solutions
TiDB Solutions by Industry
TiDB Cloud Services
ScyllaDB Consulting & Architecture Planning
ScyllaDB Administration & Maintenance
ScyllaDB Security and Privacy Maintenance
ScyllaDB Performance & Query Optimization
ScyllaDB Migration Services
ScyllaDB Backup & Disaster Recovery
ScyllaDB High Availability Solutions
ScyllaDB Solutions by Industry
ScyllaDB Cloud Services
  1. Home
  2. > Blogs
  3. > PostgreSQL
  4. > Mastering PostgreSQL Meta-Commands: The Ultimate psql Cheat Sheet

Mastering PostgreSQL Meta-Commands: The Ultimate psql Cheat Sheet

Why memorize SQL queries when \d, \l, and \dx do the heavy lifting? Learn the power of PostgreSQL’s psql meta-commands today.

Jethish June 16, 2025

Subscribe for email updates

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:

CommandDescription

\l or \list

List all databases

\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

\dc

List conversions

\dC

List casts

\dd

Show object comments

\do

List operators

\da

List aggregates

\du or \dg

List roles

\dx

List installed extensions

\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.

 

DESCRIBE COMMANDS

Drill down into the structure of your objects:

CommandDescription

\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.

 

COMMON / UTILITY COMMANDS

Everyday tools that make psql a powerhouse:

CommandDescription

\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.

 

CONDITIONAL COMMANDS

Useful in scripting scenarios:

CommandDescription

\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.

 

FORMATTING AND OUTPUT

Clean and customize your query results:

CommandDescription

\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.

 

HISTORY AND BUFFER COMMANDS

Handle your past and present commands:

CommandDescription

\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.

 

HELP COMMANDS

When in doubt, ask psql itself:

CommandDescription

\?

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.

 

EXPORT / COPY DATA EASILY

Command

Description

\COPY

Client-side CSV (or other format) import/export command

Replication related meta-commands

Command

Description

\dRp

List publications

\dRs

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

  • Combine \x with a SELECT to instantly improve readability.
  • Use \timing to benchmark queries during optimization.
  • Pipe command output using \! to integrate with your OS tools.
  • Save favorite queries in a file and run them with \i.
  • These little enhancements turn 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.

 

Author Bio

Jethish

Jethish is a PostgreSQL DBA at Mafiree with expertise in building scalable, reliable, and high-performance database infrastructures. He focuses on PostgreSQL architecture, replication strategies, performance tuning, and high availability for mission-critical systems. Through his technical writing, he shares clear, practical insights on database internals, replication choices, load balancing, and cross-database integrations that help engineers and DBAs tackle real-world data challenges.

Leave a Comment

Related Blogs

AWS Database Storage Optimization: How We Reclaimed 3.6 TB and Cut Costs in Half

A client came to us with a classic AWS database storage optimization problem: 15.2 TB allocated, less than a third actually in use — and a bill that kept growing regardless. Within one week, Mafiree had reclaimed 3.6 TB, validated a safe path to cut allocation nearly in half, and executed a zero-downtime migration. Here's the full story.

  54 views
PostgreSQL Connection Pooling: PgBouncer vs Odyssey – Performance & Configuration

PostgreSQL uses a process-per-connection model, which can limit scalability in high-traffic environments. Connection poolers help manage this challenge by reusing database connections efficiently. This blog compares PgBouncer and Odyssey, two popular PostgreSQL connection poolers, highlighting their architecture, performance characteristics, configuration differences, and ideal use cases. It helps organizations choose the right pooling solution based on workload scale, complexity, and operational requirements.

  1771 views
8 Enhancing Features in PostgreSQL 18

PostgreSQL 18: Efficiency, security, and reliability, all in one upgrade

  4076 views
Optimizing PostgreSQL Queries with Functional Indexes – A Real-World Case Study

Cutting Query Time from 10 Minutes to Under 1 Second – How Functional Indexes Helped Us Optimize Aurora PostgreSQL and Stabilize CPU Performance.

  2728 views
Choosing the Right Replication Type in PostgreSQL

Choosing the Right Replication Type in PostgreSQL – Understand the key differences between Streaming and Logical Replication, their best use cases, and how to implement them effectively for high availability, scalability, and disaster recovery.

  2228 views

Subscribe for email updates

Get in touch with us

Highlights

More than 6000 Servers Monitored

Happy Clients

Certified DBAs

24 x 7 x 365 Support

PCI

Database Services

MySQL MongoDB PostgreSQL SQL Server Aerospike Clickhouse TiDB MariaDB Columnstore

Quick Links

Careers Blog Contact Privacy Policy Disclaimer Policy

Contacts

Linkedin Mafiree Facebook Mafiree Twitter Mafiree

Nagercoil Office

Miru IT Park, Vallankumaranvillai,

Nagercoil, Tamilnadu - 629 002.

Bangalore Office

Unit 303, Vanguard Rise,

5th Main, Konena Agrahara,

Old Airport Road, Bangalore - 560 017.

Call: +91 6383016411

Email: sales@mafiree.com


Copyright © - All Rights Reserved - Mafiree