Starting the database and asking your first question
The owner of Tidewell Supply sends one line: "How many orders did we take in 2025?" The answer sits in a database, and you cannot scroll to it. You have to ask for it in SQL, the language almost every relational database understands. This lesson gets you from nothing installed to a number on your screen.
One container, one database
You will not install PostgreSQL on your laptop. You will run it in a Docker container, which keeps the course identical on Windows, macOS and Ubuntu and leaves nothing behind when you delete it. Start the server like this:
$ docker run -d --name tidewell-db -e POSTGRES_PASSWORD=learn -e POSTGRES_DB=tidewell -p 5432:5432 postgres:17The image name carries the version on purpose: postgres:17, never latest. If the image moved to a new major version overnight, your queries would run on something other than what this course tested. The POSTGRES_DB variable creates an empty database called tidewell. The two data files that fill it ship with the course, in the dataset folder: schema.sql creates the tables and seed.sql inserts the rows. Copy both into the container and run them with psql -f, which reads a file and executes it:
$ docker cp dataset/schema.sql tidewell-db:/tmp/schema.sql
$ docker cp dataset/seed.sql tidewell-db:/tmp/seed.sql
$ docker exec tidewell-db psql -U postgres -d tidewell -q -f /tmp/schema.sql
$ docker exec tidewell-db psql -U postgres -d tidewell -q -f /tmp/seed.sqlThe -q flag keeps psql quiet, so silence means success. Without it, the seed file prints one INSERT 0 400 line per batch, which is harmless but noisy. If you work in Git Bash on Windows and the copy fails, Lab 1 shows the cause and the one-line fix.
Opening psql and finding your way around
Connect with an interactive session:
$ docker exec -it tidewell-db psql -U postgres -d tidewellThe prompt tidewell=# means psql is connected to the database called tidewell and waiting. Two kinds of input work here. SQL statements end with a semicolon. Meta-commands start with a backslash, belong to psql rather than to SQL, and need no semicolon. \dt lists the tables:
tidewell=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+----------
public | categories | table | postgres
public | customers | table | postgres
public | order_items | table | postgres
public | orders | table | postgres
public | payments | table | postgres
public | products | table | postgres
public | reviews | table | postgres
(7 rows)Seven tables. \d customers describes one of them, with its column types and its relationships:
tidewell=# \d customers
Table "public.customers"
Column | Type | Collation | Nullable | Default
-----------------+---------+-----------+----------+---------
customer_id | integer | | not null |
full_name | text | | not null |
email | text | | |
country | text | | |
city | text | | |
signup_date | date | | not null |
referral_source | text | | |
Indexes:
"customers_pkey" PRIMARY KEY, btree (customer_id)
Referenced by:
TABLE "orders" CONSTRAINT "orders_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
TABLE "reviews" CONSTRAINT "reviews_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(customer_id)Every column has a type. integer, text and date are the three you will meet most. A column without not null may hold NULL, which means "unknown", and that small fact will cost you a few wrong totals before you learn to respect it. Module 3 is built around it.
Your first statement
A statement is a question the server answers. The owner's question needs two ideas: choose a table with FROM, and count its rows with count(*).
SELECT count(*) FROM orders; count
-------
2450
(1 row)That is every order since the shop opened. Adding a filter on the year comes in the next module, but you can already peek at what the table holds. The star means "all columns", and LIMIT caps the number of rows so a table of thousands does not flood your screen:
SELECT * FROM orders LIMIT 3; order_id | customer_id | order_date | status | shipped_date | discount_code
----------+-------------+------------+-----------+--------------+---------------
1 | 223 | 2024-01-04 | delivered | 2024-01-06 | VIP20
2 | 92 | 2024-01-10 | DELIVERED | 2024-01-12 |
3 | 189 | 2024-01-10 | delivered | 2024-01-11 | VIP20
(3 rows)Already something odd: the status column spells "delivered" in lowercase on one row and "DELIVERED" on another. Keep that in mind, it is deliberate.
What an error looks like
You will mistype. Everybody does, and a typo costs nothing here, so make some now and learn to read the messages. A misspelled keyword:
SELEC count(*) FROM orders;ERROR: syntax error at or near "SELEC"
LINE 1: SELEC count(*) FROM orders;
^The server points at the exact word it did not understand, and the caret marks the spot. Now a table name that is also a reserved word in SQL:
SELECT count(*) FROM order;ERROR: syntax error at or near "order"
LINE 1: SELECT count(*) FROM order;
^order belongs to ORDER BY, so the parser stops there. That is why the table is called orders. And a table that simply does not exist:
SELECT count(*) FROM order_lines;ERROR: relation "order_lines" does not exist
LINE 1: SELECT count(*) FROM order_lines;
^This one reads differently: the syntax was fine, the name was not. "syntax error" means fix the grammar, and "does not exist" means fix a name. Read the last line of an error first, it usually names the culprit.
- A container holds PostgreSQL 17 and the
tidewelldatabase;psqlis the client you type into. - Statements end with
;, meta-commands such as\dtstart with a backslash. SELECT count(*) FROM orders;counts rows,LIMITcaps what you display.- "syntax error at or near" means grammar, "does not exist" means a wrong name.
Next you will choose which columns come back and in which order.
Questions and discussion
No messages yet. Have a question about this lesson? Ask it here.
Sign in to join the discussion. Sign in