What you will do
A database keeps information in tables. A web API lets a program ask for that information over the web. You make a table in a real database. Then you read and change it with web requests sent by curl, the way a phone app talks to a server.
Before you start
Note
Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The first start takes a few seconds, because the database starts first. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.
1. Make a table
The database is called shop. This makes a table called notes with two columns: an id that counts up by itself, and the body text of each note. PGPASSWORD is the password, so you are not asked for it.
export PGPASSWORD=shopShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
psql -h db -U shop -c "create table notes (id serial primary key, body text not null)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
2. Put a note in it
insert adds a row. The second command tells the API to read the database again. The API read it once when it started, before notes existed.
psql -h db -U shop -c "insert into notes (body) values ('hello')"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
psql -h db -U shop -c "notify pgrst, 'reload schema'"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
3. Read it over the web
The API gives the table a web address. curl asks for it and the answer comes back as JSON, a text format programs read easily: [{"id":1,"body":"hello"}]. -w "\n" ends the answer with a new line, so it sits on its own line under the command.
curl -s -w "\n" http://api:3000/notesShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
4. Add a note over the web
A POST request adds a row. The -d part is the new note, as JSON. Then read the table again: there are two notes.
curl -s -X POST -H "Content-Type: application/json" -d '{"body":"from curl"}' http://api:3000/notesShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
curl -s -w "\n" http://api:3000/notesShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
5. Ask for less
Words added to the address ask for part of the table. This one asks only for the note whose body equals hello.
curl -s -w "\n" "http://api:3000/notes?body=eq.hello"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
6. See what lasts
Press Stop the lab on the lab bar and then Start the lab again, and run step 3 again: your notes are still there. The database keeps its files in a named volume, and the platform saves the volume. Anything you write outside a volume is lost when the lab stops. Containers in production behave the same way.
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is two services, a database and an API, that find each other by name, which is what a real application looks like. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.