PostgreSQL as a relational database management system
corebeginnerPostgreSQL is a server that stores data in typed tables and enforces the rules you declare on every write — a NULL, or an order for a nonexistent customer, gets refused. It protects correctness, not just accepts SQL.
Think of it as
A relational database is a strict clerk, not a filing cabinet. A filing cabinet accepts whatever you hand it. A clerk checks the form against the rules before filing it at all — right types, required fields present, references to other files that actually exist — and refuses the ones that fail.
What we're doing: Show PostgreSQL refusing writes that violate declared structure, rather than silently storing whatever arrives.
- 1–4
- Declares the shape once: id, and an email that must be present and unique.
- 6–10
- orders declares its own rules, including a foreign key back to customers and a CHECK that total cannot be negative.
- 12
- A valid customer row — passes NOT NULL and UNIQUE.
- 14
- customer_id 999 does not exist in customers yet, so the foreign key constraint rejects this row outright.
ERROR: insert or update on table "orders" violates foreign key constraint "orders_customer_id_fkey"
DETAIL: Key (customer_id)=(999) is not present in table "customers".Why this works: PostgreSQL checks every constraint declared on a table before a row is allowed to exist — types, NOT NULL, UNIQUE, CHECK and foreign keys alike. The order INSERT never reaches the table; the server rejects it at the boundary. A document store with no schema validation would have stored this row as-is, leaving the application to notice the dangling reference later, if it ever does.
Treating PostgreSQL as "just a place to run SQL"
Wrong
Better
What you see: The wrong version accepts a negative total and a customer_id that references nothing, silently. Nothing fails until application code (or a person) eventually notices the impossible row.
Why: A table with no constraints is just a typed grid — PostgreSQL will still enforce the column types, but nothing else. The invariants that make the data trustworthy (a total cannot be negative, an order must belong to a real customer) have to be declared explicitly. Skipping them turns a relational database into a fancy, slightly-typed filing cabinet, giving up the exact guarantee an RDBMS exists to provide.
- INSERT arrives — a candidate row
- constraints checked — types, NOT NULL, CHECK, foreign keys
- row stored — only if every rule passed
PostgreSQL next to three other stores
Together
Remember: PostgreSQL enforces the constraints you declare on every write — it refuses bad data rather than accepting and storing whatever SQL sends it.
See also: postgresql terminology · server database schema

