⚡ 100% In-Browser & Private: Your data never leaves your computer. Powered by Cloudflare Edge CDN.
ProWebTools PRO
Developer & SEO Utilities
30+ Commands

PostgreSQL CLI (psql) & Admin Commands Matrix

Production PostgreSQL administration reference: psql shortcuts (\l, \dt+), database sizes, terminating stuck queries, active locks, vacuum analyze, and pg_dump backup with 1-click copy.

psql Meta-Commands (10 commands)

psql Meta-Commands
psql -U <user> -d <database> -h <host> -p 5432

Connects to a PostgreSQL database instance via terminal CLI.

PRO TIP Add -W to force password prompt if not stored in ~/.pgpass.
psql Meta-Commands
\l

Lists all databases on the PostgreSQL cluster along with owner and encoding.

PRO TIP Add + (\l+) to display database disk sizes on filesystem.
psql Meta-Commands
\c <database_name>

Switches connection context to a different database.

PRO TIP Can also switch user: \c .
psql Meta-Commands
\dt

Lists all tables in the current schema.

PRO TIP Add + (\dt+) to inspect table disk size and description comments.
psql Meta-Commands
\d <table_name>

Describes table schema: columns, data types, nullability, defaults, and indexes.

PRO TIP Add + (\d+ ) to view storage types and foreign key relationships.
psql Meta-Commands
\dn

Lists all schemas within the current database.

PRO TIP Default user schema is "public".
psql Meta-Commands
\di

Lists all indexes across tables in the current schema.

PRO TIP \di+ shows physical index disk footprints.
psql Meta-Commands
\timing

Toggles query execution time measurement on/off in psql.

PRO TIP Crucial for benchmarking query execution speed directly in terminal.
psql Meta-Commands
\x

Toggles expanded table output mode (displays rows vertically as key: value).

PRO TIP Essential when inspecting tables with dozens of wide columns.
psql Meta-Commands
\q

Quits / exits the psql interactive command prompt.

PRO TIP Ctrl+D also exits cleanly.

Database & Role Management (3 commands)

Database & Role Management
CREATE DATABASE app_db WITH OWNER app_user ENCODING 'UTF8';

Creates a new database with specified owner and UTF-8 encoding.

PRO TIP Always ensure UTF-8 encoding for international character support.
Database & Role Management
CREATE USER app_user WITH ENCRYPTED PASSWORD 'strong_secret';

Creates a new PostgreSQL role with login credentials.

PRO TIP Grant minimal necessary permissions rather than granting SUPERUSER.
Database & Role Management
GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;

Grants database-level privileges to role.

PRO TIP In Postgres 15+, also grant schema privileges: GRANT ALL ON SCHEMA public TO app_user.

Performance, Locks & Diagnostics (5 commands)

Performance, Locks & Diagnostics
SELECT pid, now() - pg_stat_activity.query_start AS duration, query, state FROM pg_stat_activity WHERE state != 'idle' ORDER BY duration DESC;

Displays all currently executing active queries sorted by longest duration.

PRO TIP Identifies runaway queries hanging connections and locking tables.
Performance, Locks & Diagnostics
SELECT pg_cancel_backend(<pid>);

Gracefully cancels the currently executing query for the given process ID.

PRO TIP Does not terminate client connection; only cancels current SQL statement.
Performance, Locks & Diagnostics
SELECT pg_terminate_backend(<pid>);

Forcefully terminates the entire backend connection for the given process ID.

PRO TIP Use when a locked connection fails to respond to pg_cancel_backend.
Performance, Locks & Diagnostics
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM users WHERE email = '[email protected]';

Executes statement and prints exact query plan, timing, and buffer cache hits.

PRO TIP Look for "Seq Scan" on large tables indicating missing indexes.
Performance, Locks & Diagnostics
SELECT relname AS table_name, pg_size_pretty(pg_total_relation_size(relid)) AS total_size FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 10;

Lists the top 10 largest tables in the database including index storage.

PRO TIP Identifies tables consuming excessive disk space.

Backup, Restore & Maintenance (3 commands)

Backup, Restore & Maintenance
pg_dump -U postgres -d app_db -F c -b -v -f app_db.dump

Creates a compressed, custom-format binary backup of the database.

PRO TIP -F c allows selective table restores and multi-threaded pg_restore.
Backup, Restore & Maintenance
pg_restore -U postgres -d new_db -v -j 4 app_db.dump

Restores database from custom-format dump using 4 parallel CPU worker threads.

PRO TIP -j 4 dramatically accelerates restore times for large databases.
Backup, Restore & Maintenance
VACUUM (VERBOSE, ANALYZE);

Reclaims dead tuple disk storage and updates query planner statistics.

PRO TIP Standard autovacuum handles this in background, but manual vacuum helps after large bulk deletes.