Postgres Orientation & psql
Lesson opener
In one line: How Postgres sits next to the MySQL-flavoured SQL you already know - same job, different engine habits.
Why it matters: GUIs hide what the server actually sees. psql is the common language between analysts, DBAs, and deploy scripts.
Before this lesson: SQL & DBMS (MySQL) / sql11
Before you start
| Resource | Link |
|---|---|
| Previous | Constraints & transactions (finish SQL & DBMS (MySQL) first) |
| Next | Types & dialect that matter |
| Prerequisites | SQL & DBMS (MySQL) through sql11 — SELECT, joins, CTEs, basic windows |
| Time | ~45 minutes |
| Install | PostgreSQL & pgAdmin — server running, then the GUI. Do this first if psql is missing. |
| What you need | PostgreSQL 14+ installed locally (or a cloud sandbox), and the psql client |
What you will learn
- How Postgres sits next to the MySQL-flavoured SQL you already know — same job, different engine habits
- How to connect with
psqland use the meta-commands you will use every day - A small lab schema mindset for this track (you will reuse patterns from the SQL course, not rebuild ANSI SQL)
- Where extensions fit (awareness only — when to reach for them)
Why a second database course?
You already write SELECT, JOIN, GROUP BY, CTEs, and intro window functions. That skill travels.
What does not travel unchanged:
| Area | In the SQL course (MySQL-leaning) | In this track |
|---|---|---|
| Client | Workbench-first | psql as the daily tool |
| Types | MySQL spellings (TINYINT, IFNULL, DATE_FORMAT) | Postgres types and functions |
| Semi-structured data | Rarely used | JSONB, arrays |
| Upserts | Manual UPDATE/INSERT dances | ON CONFLICT |
| Indexes | “Create an index on this column” | B-tree vs GIN vs GiST vs BRIN |
| Plans | One-line EXPLAIN mention | Real EXPLAIN ANALYZE |
| Security | Mostly app-layer | Roles + row-level security |
This course assumes you can already pull data. We focus on what Postgres is good at and how teams run it day to day.
Install check (one minute)
If this fails, stop and do PostgreSQL & pgAdmin. pgAdmin cannot create a server that is not already running — and this course does not stay in the GUI after that install lesson.
If Postgres is installed, this should print a version:
psql --versionConnect to a database (adjust user/host if your install differs):
psql -U postgres -d postgresYou should see a prompt like postgres=#. Type \q to quit.
Workplace note: On many teams the connection string lives in a secrets manager; locally you will often use a .pgpass file or env vars. For this lab, a local superuser is fine — never paste production passwords into lesson notes.
Tutorial — Your first five minutes in psql
[Orient yourself in a new database]
WHY: GUIs hide what the server actually sees. psql is the common language between analysts, DBAs, and deploy scripts.
WHEN: You just got credentials to a Postgres box and need to learn the landscape before writing reports.
\conninfo -- where am I connected?
\l -- list databases
\c learnings_lab -- connect to a database (create it first if needed)
\dt -- list tables in the current schema
\d customers -- describe one table (columns, indexes, FKs)
\x -- toggle expanded display (great for wide rows)
\timing -- show how long each query took
\q -- quitBreaking it down:
\conninfo→ confirms host, user, database — stop guessing which environment you are in\dt→ “what tables exist?” without writing catalog SQL\d tablename→ the fastest schema reminder while you draft queries\x→ one row per screen when SELECT * is unreadable\timing→ pairs well with later EXPLAIN lessons
Create a practice database (once):
CREATE DATABASE learnings_pg;Then:
\c learnings_pgA tiny starter schema (optional)
You can recreate familiar lab ideas in Postgres. Example:
CREATE TABLE customers (
customer_id TEXT PRIMARY KEY,
segment TEXT NOT NULL,
total_revenue NUMERIC(12,2) NOT NULL DEFAULT 0,
churned BOOLEAN NOT NULL DEFAULT FALSE,
tags TEXT[] DEFAULT '{}',
profile JSONB NOT NULL DEFAULT '{}'::jsonb
);
CREATE TABLE sales (
sale_id BIGSERIAL PRIMARY KEY,
customer_id TEXT NOT NULL REFERENCES customers(customer_id),
sale_month DATE NOT NULL,
revenue NUMERIC(12,2) NOT NULL CHECK (revenue >= 0)
);You will use arrays and JSONB for real in later lessons — this just plants the columns.
Callout — Extensions (awareness only)
Extensions = optional plugins for Postgres. You do not need them for day-one SELECT. You need to know they exist so you do not reinvent them badly.
| Extension | What it is for | When people reach for it |
|---|---|---|
pg_trgm | Trigram similarity / fuzzy text | “Find customers whose name is almost this spelling”; accelerate LIKE '%foo%' patterns with the right index |
pgcrypto | Crypto helpers in SQL | Hashing tokens, encrypting sensitive columns when policy allows DB-level crypto |
| PostGIS | Spatial types and functions | Maps, distances, “stores within 5 km” — GIS workloads |
Enable pattern (once, as a privileged user):
CREATE EXTENSION IF NOT EXISTS pg_trgm;Rule of thumb: Prefer a well-maintained extension over a clever custom function you alone understand. Deeper use of these tools is out of scope for this course — know the names, the job, and when to ask a DBA.
Essential commands
| Command / action | What it does |
|---|---|
psql -U user -d db | Open an interactive session |
\l / \c db / \dt / \d table | List DBs, connect, list tables, describe table |
\x / \timing | Expanded display / show query duration |
\q | Leave psql |
CREATE EXTENSION … | Turn on an optional extension (awareness) |
Practice
Do this now:
- Connect with
psqland run\conninfo,\l, and\timing. - Create database
learnings_pgif setup6 did not already. - Create the
customers/salesstarter tables above (or your own equivalent). - Insert two customers and three sales rows with plain
INSERT. - Save your script as
exercises/answers/pg/01_orientation.sql.
Where This Connects
Scale-up: Once a single Postgres instance isn't enough, the usual first moves are connection pooling (e.g. PgBouncer) and read replicas, then managed cloud Postgres (RDS, Cloud SQL, Azure Database, Supabase-style hosts). Heavy analytics scans sometimes leave OLTP Postgres for a warehouse — same awareness note as in the SQL track. Not taught here.
Cross-ecosystem: This track follows SQL & DBMS (MySQL) — especially SQL commands. Same SELECT / join job; Postgres types and ops habits from here on.
Teacher walkthrough: psql daily loop
All figures illustrative.
Steps: Connect, list DBs/tables with meta-commands, run a simple SELECT, quit cleanly.
Failure example
Symptom: psql not found after GUI install
Diagnosis: PATH/binaries not on shell PATH; server may still be running.
Correction: Use setup6; open full path or fix PATH; verify psql --version then connect.
Validation: Re-run the lesson's verify query/EXPLAIN snippet and confirm expected plan or result.
Guided practice
You need to list tables in the current schema. Which meta-command family?
Complete solution: \dt (and \dn/\d as taught) — not a SQL invent-your-own SHOW in MySQL dialect.
Independent practice
Acceptance criteria:
- Completed the lesson's practice queries on your lab database.
- One failure you hit (or a simulated one) documented with fix.
- Notes state when not to use this feature.
- Link forward to the next
pglesson.
Common mistakes
- Copying MySQL dialect unchanged.
- Skipping
EXPLAINwhen performance is the claim. - Changing production without a transaction/backup habit (pg10).
Checklist
- [ ] I can open
psqland confirm which database I am on - [ ] I used
\dtand\dwithout opening a GUI - [ ] I know
pg_trgm/pgcrypto/ PostGIS at “what/when” level - [ ] Next: Types & dialect that matter