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)

\d TABLE_NAME

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

\d+

List tables from the current schema (psql)

\dt 

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
schemaname='__schema_name__';

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;
EXPLAIN SELECT * FROM post LIMIT 50;
SELECT count(*) AS exact_count FROM myschema.mytable;
\l+
\l+ <database_name>

--

--

Building engagebud.com, 📍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