Unlock the power tools that set PostgreSQL apart: JSONB for semi-structured data, arrays, UUID generation, schemas for organization, import/export with COPY, and identity columns. This lesson is fully PostgreSQL-specific and continues the compact Module 4 schema in the Docker container.
Unlike SQLite's flexible typing, PostgreSQL enforces types strictly. This catches bugs at the database level rather than in your application code.
docker exec -it pg-sql-course psql -U postgres -d devops_sql-- SQLite would accept this, PostgreSQL won't:
-- INSERT INTO products (name, price, category) VALUES ('Test', 'not-a-number', 'Test');
-- ERROR: invalid input syntax for type numeric: "not-a-number"-- PostgreSQL type casting:
SELECT '42'::INTEGER, '3.14'::NUMERIC, 'true'::BOOLEAN;-- Check what types PostgreSQL supports:
SELECT typname FROM pg_type WHERE typtype = 'b' ORDER BY typname LIMIT 20;-- Common PostgreSQL types vs SQLite:
-- PostgreSQL | SQLite
-- INTEGER | INTEGER
-- NUMERIC(10,2) | REAL (no precision control)
-- VARCHAR(255) | TEXT (length ignored)
-- BOOLEAN | INTEGER (0/1)
-- TIMESTAMP WITH TZ | TEXT
-- JSONB | TEXT
-- UUID | TEXT
SELECT 1::BOOLEAN, 0::BOOLEAN, 'hello'::VARCHAR(3);PostgreSQL's strict typing means the database itself prevents invalid data. You can't store a string where a number is expected, you can't store an invalid date, and boolean values are real booleans (not integers). The :: operator casts between types. PostgreSQL will truncate VARCHAR to the specified length and raise errors for truly incompatible types. This is a major upgrade from SQLite's 'store anything anywhere' approach.
Type casting queries succeed. '42'::INTEGER returns the integer 42. 1::BOOLEAN returns true. 'hello'::VARCHAR(3) returns 'hel' (truncated). The pg_type query shows PostgreSQL's built-in type catalog.