Arguably the most important part of your typical web application is the storage of data. It would be pretty useless if each time you logged into your account on YouTube, Twitter or GitHub, all of your subscriptions, tweets, or repositories were gone.
When you run a program on your computer (like our HTTP server), the program is loaded into memory. Memory is a lot like a scratch pad. It's fast, but it's not permanent. If the program terminates or restarts, the data in memory is lost.
When you're building a web server, any data you store in memory (in your program's variables) is lost when the server is restarted. Any important data needs to be saved to disk via the file system.
We could take our user's data, serialize it to JSON, and save it to disk in .json
files (or any other format for that matter). It's simple, and will even work for small applications. Trouble is, it will run into problems fast:
At the end of the day, a database technology like MySQL, PostgreSQL, or MongoDB "just" writes files to disk. The difference is that they also come with all the fancy code and algorithms that make managing those files efficient and safe. In the case of a SQL database, the files are abstracted away from us entirely. You just write SQL queries and let the DB handle the rest.
We will be using option 2: PostgreSQL. It's a production-ready, open-source SQL database. It's a great choice for many web applications, and as a back-end engineer, it might be the single most important database to be familiar with.
macOS with brew
brew install postgresql@15
Linux / WSL (Debian). Here are the docs from Microsoft, but simply:
sudo apt update
sudo apt install postgresql postgresql-contrib
psql --version
sudo passwd postgres
Enter a password, and be sure you won't forget it. You can just use something easy like postgres
.
brew services start postgresql@15
sudo service postgresql start
Enter the psql
shell:
psql postgres
sudo -u postgres psql
You should see a new prompt that looks like this:
postgres=#
CREATE DATABASE chirpy;
\c chirpy
You should see a new prompt that looks like this:
chirpy=#
ALTER USER postgres WITH PASSWORD 'postgres';
For simplicity, I used postgres
as the password. Before, we altered the system user's password, now we're altering the database user's password.
From here you can run SQL queries against the chirpy
database. For example, to see the version of Postgres you're running, you can run:
SELECT version();
Run and submit the CLI tests.
The Boot.dev CLI requires you to be signed in to submit your solution!
Copy/paste one of the following commands into your terminal:
Run
bootdev run c3e215a5-1d8f-437b-9f89-3606118800ec
Submit
bootdev run c3e215a5-1d8f-437b-9f89-3606118800ec -s
To run and submit the tests for this lesson, you must have an active Boot.dev membership
Using the Bootdev CLI
The Bootdev CLI is the only way to submit your solution for this type of lesson. We need to be able to run commands in your environment to verify your solution.
You can install it here. It's a Go program hosted on GitHub, so you'll need Go installed as well. Instructions are on the GitHub page.