Skill-
Forge
Learn
Notes
Language & Framework notes
CS Core Subjects
OS, DBMS, Networks, OOP & more
System Design
Architecture & High-Scale
Roadmaps
Guided developer learning paths
Cheat Sheets
Quick syntax references
Resources
Curated books, guides & links
Practice
Problems
DSA & coding challenges
Quizzes
Test your knowledge
Algorithms
Explanations & visualizations
Git Visualizer
Interactive Git graph & CLI playground
Aptitude
Placement & logic prep
Formula Simulators
Real-time quant equation simulators
Build
Projects Hub
Step-by-step real world projects
Resume Builder
ATS-friendly resumes, live preview & score
Tools
Online compilers & utilities
Skills
Core professional & tech skills
Error Encyclopedia
Search
⌘K
Install App
Start Learning
Menu
Learn
Notes
CS Core Subjects
System Design
Roadmaps
Cheat Sheets
Resources
Practice
Problems
Quizzes
Algorithms
Git Visualizer
Aptitude
Formula Simulators
Build
Projects Hub
Resume Builder
Tools
Skills
Error Encyclopedia
Search
Install Skill-Forge App
Appearance
Start Learning
Skip to main content
Cheatsheets
20 sections
64 cards
v16 · psql CLI · ACID · 2025
🐘
PostgreSQL Cheat Sheet
DDL · DML · Joins · Window Functions · CTEs · Indexes · JSONB · Performance
Browse sections
Shortcuts
?
Hide Sidebar
Search cheatsheet sections
/
or
⌘K
Progress
0%
Table of contents
Shortcuts
?
⌨️
psql CLI
🔢
Data Types
🏗️
DDL — Data Definition Language
✏️
DML — Insert, Update, Delete, Upsert
🔍
SELECT & Filtering
🔗
JOINs
📊
Aggregates & GROUP BY
🪟
Window Functions
📐
CTEs & Subqueries
🔒
Constraints
⚡
Indexes
🔄
Transactions & ACID
👁️
Views & Materialized Views
🧩
Functions & Stored Procedures
⚙️
Triggers
📦
JSON & JSONB
🔎
Full-Text Search
🚀
EXPLAIN & Performance
👤
Roles & Security
📋
Quick Reference
J
K
Jump
/
Search
Esc
Clear
⌨️
psql CLI
Interactive Terminal
Connect & Launch
Meta-commands (backslash) 🔥
COPY — Import & Export
🔢
Data Types
PostgreSQL-specific Types
Type Reference 🔥
Type Casting
Arrays
🏗️
DDL — Data Definition Language
CREATE · ALTER · DROP
CREATE TABLE 🔥
ALTER TABLE
DROP & Schemas
Custom Types & Enums
✏️
DML — Insert, Update, Delete, Upsert
Data Manipulation
INSERT
UPSERT — ON CONFLICT ⭐
UPDATE & DELETE
🔍
SELECT & Filtering
Query Fundamentals
SELECT Clause Order 🔥
WHERE Operators
DISTINCT, LIMIT & FETCH
Useful Scalar Functions
Date & Time Functions
🔗
JOINs
All Join Types
Join Types Visual 🔥
JOIN Syntax
SELF JOIN & LATERAL
USING & NATURAL
📊
Aggregates & GROUP BY
Summarize Data
Aggregate Functions 🔥
GROUP BY Patterns
Useful Aggregation Patterns
🪟
Window Functions
Analytics Without Collapsing Rows
Window Function Syntax 🔥
Ranking Functions
Value & Navigation Functions
Running Totals & Moving Avg ⭐
📐
CTEs & Subqueries
WITH · Recursive · Correlated
CTEs (Common Table Expressions) 🔥
Subqueries
Set Operations
🔒
Constraints
Data Integrity Rules
Constraint Types 🔥
Foreign Key Actions
Partial & Expression Constraints
⚡
Indexes
B-Tree · GIN · GiST · BRIN
Index Types 🔥
CREATE INDEX Syntax
Index Management
Covering Index (INCLUDE) ⭐
🔄
Transactions & ACID
BEGIN · COMMIT · ROLLBACK
ACID Properties
Transaction Commands
Isolation Levels
Locking
👁️
Views & Materialized Views
Saved Queries & Cached Results
Regular Views
Materialized Views ⭐
🧩
Functions & Stored Procedures
PL/pgSQL
PL/pgSQL Function 🔥
Stored Procedure (no return)
Function Volatility & Language
⚙️
Triggers
Automatic Side Effects
Trigger Pattern 🔥
📦
JSON & JSONB
Semi-structured Data
JSONB Operators 🔥
Query JSONB
Modify & Build JSONB
🔎
Full-Text Search
tsvector · tsquery · Ranking
Basic FTS
FTS with Generated Column + GIN Index 🔥
🚀
EXPLAIN & Performance
Query Analysis & Tuning
EXPLAIN & Node Types 🔥
Key Performance Queries
VACUUM & ANALYZE
Tuning Tips ⭐
<b>shared_buffers</b> — set to 25% of RAM (e.g. <code>2GB</code> on 8GB server)
<b>work_mem</b> — memory per sort/hash; set 16–64MB; watch for many connections
<b>effective_cache_size</b> — estimate of OS cache; set 75% of RAM
<b>enable_seqscan=off</b> — force index use in session for testing
Use <code>EXPLAIN (ANALYZE, BUFFERS)</code> to find buffer hits vs disk reads
Always <code>ANALYZE</code> after bulk load — stale stats = bad plans
Use <code>CREATE INDEX CONCURRENTLY</code> in production — no table lock
Use <code>CLUSTER table_name USING index_name</code> to physically reorder rows
Connection pooling with <b>PgBouncer</b> — PostgreSQL handles many connections poorly
👤
Roles & Security
Users · Privileges · RLS
Roles & Users
GRANT & REVOKE
Row-Level Security (RLS) ⭐
📋
Quick Reference
Commands at a Glance
Master SQL Cheat Table 🔥
PostgreSQL Gotchas ⭐
<b>NULL comparisons</b> — <code>NULL = NULL</code> is <code>NULL</code>, not true. Use <code>IS NULL</code> or <code>IS DISTINCT FROM</code>
<b>SERIAL vs IDENTITY</b> — prefer <code>GENERATED ALWAYS AS IDENTITY</code> (SQL standard, more control)
<b>TIMESTAMPTZ vs TIMESTAMP</b> — always use <code>TIMESTAMPTZ</code>; it stores UTC and converts per session timezone
<b>JSON vs JSONB</b> — always use JSONB; it's indexed, faster to query, removes key duplicates
<b>LIKE with index</b> — only <code>LIKE 'prefix%'</code> uses B-tree. For <code>%suffix%</code> use <code>pg_trgm</code> GIN index
<b>UNION vs UNION ALL</b> — UNION deduplicates (slow!). Use UNION ALL unless you need dedup
<b>Arrays are 1-indexed</b> — <code>arr[1]</code> is first element, not <code>arr[0]</code>
<b>WHERE vs HAVING</b> — WHERE filters rows before grouping; HAVING filters groups after
<b>UPDATE with JOIN</b> — use <code>UPDATE a ... FROM b WHERE a.id=b.id</code> (not JOIN syntax)
<b>VACUUM bloat</b> — PostgreSQL doesn't reuse dead rows across transactions until VACUUM runs
<b>SELECT * in production</b> — avoid in views/apps; schema changes silently break queries
<b>LIMIT without ORDER BY</b> — returns arbitrary rows; always add ORDER BY for deterministic paging
Common Extensions