Postgres CheatSheet

Cheatsheet containing Postgres Commands you need, Missing Anything? Kindly add a response or comment will add that 😄.

Show table definition including indexes, constraints & triggers (psql)


More detailed table definition including description and physical disk size (psql)


List tables from the current schema (psql)


List tables from all schemas (psql)

\dt *.*

List the tables in a specific schema (psql)

\dt <name-of-schema>.*

Export a table as CSV (psql)

\copy (SELECT * FROM __table_name__) TO 'file_path_and_name.csv' WITH CSV

Show table indexes (SQL)

SELECT * FROM pg_indexes WHERE tablename='__table_name__' AND

Analyze a table and store the results in the pg_statistic system catalog (SQL)

ANALYZE [__table__]

Comment on table (SQL)

Comment on table employee is 'Stores employee records';

Comment on column (SQL)

Comment on column employee.ssn is 'Employee Social Security Number';
SELECT reltuples AS card FROM pg_class WHERE relname = '<table_name>';
SHOW max_connections;
SELECT sum(numbackends) FROM pg_stat_database;
SELECT count(*) AS exact_count FROM myschema.mytable;
\l+ <database_name>



Building, 📍Bengaluru

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store