# Day 20: brew install postgresql -SQL on M1 Without SQL Server

For 19 Days my data has come from files: CSVs for pandas, PDFs for my RAG pipeline. Day 20 starts a 30-hour SQL course, and its first argument is that companies don't keep their data in files at all.

So before writing a single `SELECT`, I needed a database on my M1.

### Input Theory: Database, DBMS, Server, and SQL

The course splits "a database" into four pieces, and this was the clearest version I've seen:

| Piece | What it is |
|---|---|
| Database | The container that stores and organises the data |
| SQL | The language you use to talk to it |
| DBMS | The software that manages every request, including who's allowed to run what |
| Server | The machine it all runs on, up 24/7 |

Why not just files? Three reasons from the course. Answering one question across many files means opening them one by one. Spreadsheets break on really large data. And a database lets you control who can see what.

#### Relational vs NoSQL

My understanding so far: a "SQL database" means a **relational** database. Data sits in tables of rows and columns, with relationships between the tables. Everything else gets grouped under **NoSQL**:

- **Key-value:** a giant dictionary (Redis, Amazon DynamoDB)
- **Column-based:** groups data by column instead of by row
- **Graph:** built around the connections between data points
- **Document:** whole documents, where a fixed structure matters less (MongoDB)

The course sticks to relational databases. SQL Server, MySQL and PostgreSQL are all relational.

#### How It's Organised

A server hosts several databases, for example one for sales and one for HR. Inside each database, **schemas** work like folders that group related tables. A table has **columns** (fields), which define what's stored, and **rows** (records), which hold the actual data. One column is the **primary key**, a unique ID for every row. The course calls it a fingerprint.

Every column has a data type too: `INT`, `DECIMAL`, `DATE`, `TIME`, and for text, `CHAR` vs `VARCHAR`. `CHAR(5)` always reserves five characters. `VARCHAR` only takes what the value needs.

#### Three Families of Commands

| Family | Commands | What it does |
|---|---|---|
| DDL (Data Definition Language) | `CREATE`, `ALTER`, `DROP` | Defines the structure |
| DML (Data Manipulation Language) | `INSERT`, `UPDATE`, `DELETE` | Changes the data |
| DQL (Data Query Language) | `SELECT` | Asks questions |

According to the course, most of the 30 hours go into that last row.

### Process to Code: brew install postgresql

The course runs on Microsoft SQL Server Express, managed with SQL Server Management Studio (SSMS), and both are Windows tools. SQL Server has no native Apple Silicon build. On an M1 it only runs inside Docker under Rosetta emulation.

The course download also includes PostgreSQL versions of both practice databases, next to the SQL Server and MySQL ones. That made PostgreSQL the easy choice on a Mac, because it runs natively on M1. I installed it with Homebrew:

```bash
brew install postgresql@19
echo 'export PATH="/opt/homebrew/opt/postgresql@19/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
brew services start postgresql@19
psql postgres
```

Homebrew doesn't link versioned PostgreSQL formulas into your PATH, which is why the `export` line is there. Without it, `psql` isn't found. `brew services` keeps the server running in the background. Homebrew also creates a database role named after your macOS user, so `psql postgres` connected without asking for a password.

Here's how the course's setup maps onto mine:

| Course (SQL Server) | Me (PostgreSQL on M1) |
|---|---|
| SQL Server Express | PostgreSQL 19 from Homebrew |
| SSMS | `psql` in the terminal |
| Windows authentication | A role named after my macOS user |
| Object Explorer → Refresh | `\l` for databases, `\dt` for tables |
| Paste the script → Execute | `createdb`, then pipe the script into `psql` |

### Train Model: Loading MyDatabase and SalesDB

There's no model today, so this step is loading the data and checking it's there. The course uses two databases: **MyDatabase**, a small one with `customers` and `orders`, and **SalesDB**, a bigger one with `customers`, `employees`, `products`, `orders` and `ordersarchive` for the later chapters.

The PostgreSQL scripts live in `01_datasets/02_postgres/`. Each one starts by creating its database, then expects you to switch into it by hand before running the rest. In `psql` it was simpler to create the database myself and send it everything except those first lines:

```bash
cd 01_datasets/02_postgres

createdb MyDatabase
sed '/DATABASE/d' init-postgres-mydatabase.sql | psql -d MyDatabase

createdb salesdb
sed '/DATABASE/d' init-postgres-salesdb.sql | psql -d salesdb
```

`sed '/DATABASE/d'` drops the lines that create and drop the database, so the tables land in the right place.

Then I checked both from inside `psql`:

```sql
\l                              -- both databases are listed
\c MyDatabase
\dt                             -- customers, orders
SELECT * FROM customers;

\c salesdb
\dt sales.*                     -- the five sales tables
SELECT * FROM sales.customers;
```

Nothing broke. Both databases loaded on the first run, and both `SELECT`s came back with rows.

> **Pro Tip:** SalesDB keeps its tables in a `sales` schema, the "folder" from the theory section. A plain `\dt` shows nothing there, and `SELECT * FROM customers` fails in `salesdb`. Use `\dt sales.*` and `sales.customers`.

### Wrap-Up

Today's biggest takeaway: you don't need SQL Server to follow a SQL Server course on an M1, because Homebrew's PostgreSQL 19 and the course's Postgres scripts got me to a working `SELECT` without Docker.

Next up: `SELECT`, `WHERE`, `ORDER BY`, `GROUP BY` and `HAVING`, and the first place T-SQL and PostgreSQL disagree, `TOP` vs `LIMIT`.

---

*Part of **My Journey with Machine Learning**, where I learn ML in public. Code lives in the `ML-Journey` repo.*
