PostgreSQL 18 cheatsheet

PostgreSQL 18: Inspecting the database

9 common PostgreSQL 18 tasks.

List databases

Show every database on the server. Run \l inside psql or use the flag from the shell.

psql -U postgres -l

Switch database

Connect to another database from inside psql.

\c mydb

List tables

Show the tables in the current schema search path.

\dt

Describe a table

Show a table's columns, types, indexes and constraints.

\d users

List schemas

Show the schemas in the current database.

\dn

List roles

Show database roles and their attributes.

\du

Toggle expanded output

Show each row as a vertical list, which is easier to read for wide tables.

\x

Show how long queries take

Toggle timing output after each statement.

\timing

Check a database's size

Show the on-disk size of a database in a readable unit.

SELECT pg_size_pretty(pg_database_size('mydb'));

Was this page helpful?