Relational Databases with PostgreSQL

Tables, keys, types and constraints in a real database, and how to load and explore data locally with PostgreSQL.

Beginner · about 5 hours · difficulty 2/5

Learn first: Joins and Combining Tables

Having your own database to practise on changes how quickly you learn SQL. PostgreSQL is free, standards-compliant and widely used, so it makes an excellent home lab.

Core concepts

  • Tables of typed columns: integer, numeric, text, date, timestamp with time zone, boolean, jsonb.
  • Primary keys uniquely identify rows; foreign keys link tables and enforce that referenced rows exist.
  • Constraints (NOT NULL, UNIQUE, CHECK) encode business rules in the database itself.
  • Schemas group tables (e.g. raw, staging, analytics).

Getting data in

  • CREATE TABLE then COPY (or psql's \copy) from CSV.
  • GUI clients like pgAdmin or DBeaver help you browse tables and run queries.
  • Alternatives for quick local work: SQLite (a single file) and DuckDB (fast analytical SQL directly on CSV and Parquet files).

Views and permissions

Views save a query as a virtual table, great for sharing a cleaned dataset. As an analyst you usually have read-only access to production replicas or warehouses, which is a good thing: never run exploratory queries against a live transactional database without permission.

OLTP vs OLAP

Transactional (OLTP) databases handle many small reads and writes for apps. Analytical (OLAP) systems handle big scans and aggregations. The same SQL works in both, but the performance characteristics differ.

Pitfall: storing numbers or dates as text on import, which breaks sorting and maths. Move on when you can create tables with sensible types and load a CSV into them.

Key concepts

  • Tables and data types
  • Primary and foreign keys
  • Constraints
  • Loading CSV with COPY
  • Views
  • SQL clients: pgAdmin, DBeaver
  • SQLite and DuckDB for local work
  • OLTP vs OLAP

Mini project

Install PostgreSQL, design three tables for a public dataset (e.g. Olist customers, orders, items), load the CSVs with COPY, add keys, and create a view of completed orders with customer city.

Check yourself

What does a foreign key protect you from, and why might analytics tables in a warehouse skip them?

Free resources