TExcelTAD
Home › PostgreSQL › PostgreSQL Intro

PostgreSQL Intro

PostgreSQL (often just "Postgres") is a free, open-source relational database management system. It stores data in tables made of rows and columns, and you talk to it with SQL, the Structured Query Language. Postgres has been developed in the open since 1986 (it grew out of the POSTGRES project at the University of California, Berkeley) and is today one of the most widely used databases in the world, from hobby projects to banks, map services and large web applications.

Why learn PostgreSQL?

Strength What it means for you
Standards-friendly SQL What you learn here transfers to other SQL databases
Reliable Full ACID transactions: a change is either completely saved or not at all
Rich data types Dates, intervals, money-safe numeric, arrays, jsonb, ranges, UUIDs and more
Extensible Write your own functions in PL/pgSQL, add indexes, types and extensions
Free Liberal open-source licence, no paid edition with hidden features

How this course works

Every lesson contains runnable playgrounds. When you press ▶ Run, your SQL is sent to a real PostgreSQL 18 server compiled to WebAssembly (the PGlite project) that runs entirely in your browser tab. Nothing is installed and nothing leaves your computer.

Each run starts with a fresh database that already contains TAD Books, a small online bookshop with authors, books, customers and orders. You will meet it properly in The Sample Database. Because the database is reset before every run, you can experiment freely: insert rows, drop tables, make mistakes. The next run starts clean.

Here is your first query. It asks the server for a little arithmetic and a greeting:

pgsql · try it yourself
SELECT 'Hello, PostgreSQL!' AS greeting, 6 * 7 AS answer;
      greeting      | answer
--------------------+--------
 Hello, PostgreSQL! |     42
(1 row)

The output looks the way PostgreSQL's command-line client psql prints results: a header line, a separator, the rows, and a footer with the number of rows.

And here is a query against the sample data: the five cheapest books in the shop.

pgsql · try it yourself
SELECT title, price
FROM books
ORDER BY price
LIMIT 5;
       title        | price
--------------------+-------
 Moon Socks         |  7.50
 Ten Tiny Dragons   |  8.99
 Murder at Low Tide | 11.99
 Harbor Lights      | 12.50
 The Map Thief      | 12.99
(5 rows)

Don't worry about the keywords yet; each one gets its own lesson.

What is a relational database?

A relational database keeps data in tables. Each table describes one kind of thing (books, customers, orders). Each row is one item and each column is one property of that item. Tables are linked by keys: an order row stores the id of the customer who placed it, so the database can join the two together.

text
customers                    orders
+----+------------+          +----+-------------+------------+
| id | first_name |          | id | customer_id | order_date |
+----+------------+          +----+-------------+------------+
|  1 | Anna       | <------- |  1 |           1 | 2024-01-05 |
|  2 | Ben        | <------- |  2 |           2 | 2024-01-19 |
+----+------------+          +----+-------------+------------+

SQL is a conversation

You don't tell Postgres how to find data; you describe what you want and the query planner decides the fastest way to get it. Statements end with a semicolon, and a single run can contain many statements. They execute one after another, just as if you typed them into psql:

pgsql · try it yourself
SELECT count(*) AS books_in_shop FROM books;
SELECT count(*) AS customers FROM customers;
 books_in_shop
---------------
            20
(1 row)

 customers
-----------
        15
(1 row)

Note: If a statement fails, PostgreSQL prints an ERROR: line and psql carries on with the next statement. You will see that behaviour in later lessons.

What you will learn

  1. Basics: creating tables, inserting, reading, updating and deleting rows.
  2. Querying: filtering, sorting, aggregates, pattern matching and joins.
  3. Schema design: data types, constraints, indexes, views and sequences.
  4. Advanced SQL: subqueries, CTEs, window functions, JSONB, arrays and full-text search.
  5. Programming the server: transactions, upserts, PL/pgSQL functions, triggers and roles.

Test yourself

What kind of database is PostgreSQL?

  1. A key-value store
  2. A relational database
  3. A document-only database
  4. A spreadsheet program

In a table, what does one row usually represent?

  1. One property of every item
  2. One item, such as one book or one customer
  3. The whole database
  4. A query

What character ends an SQL statement?

  1. A full stop .
  2. A colon :
  3. A semicolon ;
  4. A new line