PostgreSQL (psql) Commands, Operators, and Functions
Basic psql Meta-Commands
\l - List all databases
\c dbname - Connect to a database
\dt - List all tables in the current schema
\d tablename - Describe a table
\du - List all roles
\q - Quit the psql shell
SQL Basics (Common to SQL, MySQL, PostgreSQL)
SELECT * FROM table; - Retrieve all records
INSERT INTO table (col1, col2) VALUES (...);
UPDATE table SET col1 = val WHERE ...;
DELETE FROM table WHERE ...;
CREATE TABLE table (...);
DROP TABLE table;
PostgreSQL-Specific Features
SERIAL - Auto-incrementing integer
JSON / JSONB - JSON data storage
ARRAY - Store arrays in a column
RETURNING clause - Return values after insert/update/delete
UPSERT using ON CONFLICT - INSERT ... ON CONFLICT DO UPDATE ...
CTEs (WITH clause) - Common Table Expressions
Data Types in PostgreSQL
PostgreSQL (psql) Commands, Operators, and Functions
INTEGER, SERIAL, BIGINT
VARCHAR(n), TEXT
BOOLEAN
DATE, TIME, TIMESTAMP
JSON, JSONB
ARRAY
Common Operators
= <> != < > <= >= - Comparison
AND OR NOT - Logical
LIKE, ILIKE - Pattern matching (ILIKE = case-insensitive)
IS NULL, IS NOT NULL - Null checks
IN (val1, val2, ...) - Match multiple values
Useful PostgreSQL Functions
String: LOWER(), UPPER(), LENGTH(), SUBSTRING()
Math: ROUND(), CEIL(), FLOOR(), POWER(), RANDOM()
Date/Time: NOW(), CURRENT_DATE, AGE(), EXTRACT()
JSON: ->, ->>, #>, #>> (for JSON/JSONB)
Aggregate: COUNT(), SUM(), AVG(), MAX(), MIN()
Recommended YouTube Resources
1. Programming with Mosh - PostgreSQL Tutorial for Beginners
https://www.youtube.com/watch?v=qw--VYLpxG4
2. Amigoscode - PostgreSQL Crash Course
PostgreSQL (psql) Commands, Operators, and Functions
https://www.youtube.com/watch?v=Qp6xJb-uxzo
3. The Net Ninja - PostgreSQL Tutorial Series
https://www.youtube.com/playlist?list=PL4cUxeGkcC9jLYyp2Aoh6hcWuxFDX6PBJ