Setting Up SQL
You can start writing SQL right now, without installing anything. Every example in this course has a Try it button that runs the examples on PostgreSQL in a browser-based environment. When you want a database of your own, to keep your work or practice on your own data, this lesson shows how to install PostgreSQL, connect to it, and load the course's Fee Tracker database.
In One Minute
- Try it runs your SQL in your browser, on a PostgreSQL engine already loaded with the Fee Tracker data. There's nothing to install, and your queries aren't sent to a server to run.
- To work locally, install PostgreSQL (free, for macOS, Windows and Linux), then check it with
psql --versionandpg_isready. - psql is PostgreSQL's command-line program for typing SQL. A graphical client such as pgAdmin or DBeaver does the same with windows and buttons.
- Load the course data by running the
fee-tracker.sqlscript in a new database. - SQL statements end with a semicolon. psql waits until it sees one.
Option 1: Try It in Your Browser
Press Try it under any example and the Try it editor opens. On the left is a small project panel with the example's file, query.sql; on the right is the editor holding the query; at the top right is the green Run button; and underneath is the output panel. Change the query, press Run (or Ctrl+Enter, Cmd+Enter on a Mac, or Shift+F10), and the result appears in the panel. Reset puts the original example back.
Try it now with this query. It asks the database which version of PostgreSQL it is. You should see a version that starts with 18; the number after the dot changes as PostgreSQL releases updates:
SHOW server_version; server_version
----------------
18.7
(1 row)Three things are worth knowing about Try it:
| Fact | What it means for you |
|---|---|
| It's a real database | Errors, results and behavior are exactly what PostgreSQL does on a server |
| It runs in your browser | The first run downloads the database engine (a few megabytes) from this site once; after that, runs take a moment. Your SQL runs there and isn't sent to a server |
| Every run starts fresh | Each press of Run gets a new copy of the Fee Tracker data, even in the same editor. Changes from one run, including new tables and rows, are gone in the next, so you can insert, update and delete without breaking anything |
Option 2: Install PostgreSQL on Your Computer
PostgreSQL is free and open source. Pick your system. Each set of steps installs two things: the database server, which stores the data and runs the queries, and psql, the command-line client you type into.
# Install with Homebrew (https://brew.sh)
brew install postgresql@18
brew services start postgresql@18
# Make psql and the other tools available in your terminal
echo 'export PATH="$(brew --prefix postgresql@18)/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc1. Download the installer from https://www.postgresql.org/download/windows/
2. Run it and keep the default parts: PostgreSQL Server, pgAdmin 4 and Command Line Tools.
3. Choose a password for the "postgres" user and write it down.
4. Keep the default port, 5432, and finish the installer.
5. Open "SQL Shell (psql)" from the Start menu to connect.# Installs your distribution's own PostgreSQL release (may be older than 18)
sudo apt update
sudo apt install postgresql
# Create a database user with your login name that may create databases,
# so psql works without sudo. It is not a superuser.
sudo -u postgres createuser --createdb "$USER"
createdb "$USER"On Linux there are two ways to install, and they give different versions:
| Way | What you get | When to use it |
|---|---|---|
| Your distribution's package (the steps above) | Whatever release your Ubuntu or Debian version ships, which may be older than 18 | Fine for the first levels of this course; everything here works on any recent release |
| The PostgreSQL Apt Repository | PostgreSQL 18 itself, kept up to date by the PostgreSQL project | When you want the same version the course uses |
For the second way, follow the official steps for Ubuntu or Debian. They add the repository, then you install the package named postgresql-18. After that, run the same createuser and createdb lines as above.
Check That It Works
Installing the client and running the server are two different things, so check both. In a terminal (on Windows, a Command Prompt opened in C:\Program Files\PostgreSQL\18\bin, or any terminal once that folder is on your Path):
psql --version
pg_isready| Command | What it checks | A good answer looks like |
|---|---|---|
psql --version | The client is installed and on your path | psql (PostgreSQL) 18. and an update number |
pg_isready | The server is running and accepting connections | A line ending in accepting connections |
If psql --version works but pg_isready says no response, the client is installed but the server isn't running. On macOS, run brew services list to see its status and brew services restart postgresql@18 to start it again; if it still won't start, the log file named in brew info postgresql@18 says why. On Windows, the server runs as a Windows service named after the PostgreSQL version; start it from the Services app. On Linux, run sudo systemctl status postgresql.
Connect with psql
psql is a program where you type SQL and see the results as text. Open a terminal (on Windows, open SQL Shell (psql) and press Enter to accept each default, then type your password) and start it:
psql -d postgresThe prompt shows the database you're connected to, followed by =# or =>:
postgres=#The last character tells you what kind of user you are. =# means you're connected as a superuser, who may do anything; => means an ordinary user. On macOS with Homebrew and on Windows as postgres you'll usually see =#; with the Linux steps above you'll see =>, which is all this course needs.
On Windows, a quick test that the connection works is to type SELECT version(); and press Enter. One row comes back naming your PostgreSQL version.
Type SQL at the prompt and end it with a semicolon. psql also has its own short commands that start with a backslash. These are psql features, not SQL, so they have no semicolon:
| Command | What it does |
|---|---|
\l | List all databases |
\c feetracker | Connect to the database named feetracker |
\dt | List the tables in the current database |
\d students | Describe the students table: its columns, types and keys |
\i fee-tracker.sql | Run every statement in a file |
\? | Show help for all backslash commands |
\q | Quit psql |
Load the Fee Tracker Database
The course's sample data comes as one SQL script. It creates the five tables and fills them with the same rows Try it uses.
- Download fee-tracker.sql and note the folder it's saved in.
- In psql, create a database for it and connect to it. The first line is SQL; the second,
\c, is a psql command, so it works only in psql, not in pgAdmin, DBeaver or other clients. Here is the whole exchange, with what you type after each prompt and what psql answers:
postgres=# CREATE DATABASE feetracker;
CREATE DATABASE
postgres=# \c feetracker
You are now connected to database "feetracker" as user "you".
feetracker=#- Run the script with
\i, giving the full path to the file. Use forward slashes, even on Windows, and replaceyouand the folder with your own user name and download location:
\i /Users/you/Downloads/fee-tracker.sql\i C:/Users/you/Downloads/fee-tracker.sql\i /home/you/Downloads/fee-tracker.sqlpsql prints one line for each statement in the script: CREATE TABLE five times (for cities, students, courses, enrollments and payments), then INSERT 0 2, INSERT 0 3, INSERT 0 6, INSERT 0 7 and INSERT 0 7 as the rows go in. Each INSERT line ends with the number of rows it added.
- Check that everything arrived. This query counts the rows in the
coursestable; your local database should show the same answer as Try it. (count(*)is SQL, so this one also works in a graphical client.)
SELECT count(*) FROM courses; count
-------
6
(1 row)From now on, start psql with psql -d feetracker to go straight to the course data.
A Graphical Client
If you prefer windows and buttons, use a graphical client. It connects to the same PostgreSQL server and runs the same SQL; it just shows tables in a grid and lets you browse the schema with the mouse.
| Client | Cost | Notes |
|---|---|---|
| pgAdmin | Free | Made by the PostgreSQL community; installed with PostgreSQL on Windows |
| DBeaver Community | Free | Works with PostgreSQL, MySQL, SQL Server, Oracle, SQLite and more |
| A code editor extension | Free | Editors such as Visual Studio Code have PostgreSQL extensions |
To connect any of them, create a new PostgreSQL connection with host localhost, port 5432, database feetracker, and your user name and password. Remember that backslash commands such as \c and \i belong to psql: in a graphical client you pick the database from the connection settings and open a script file from its menu instead. A step-by-step walkthrough of a graphical client comes in a later lesson.
Using Another Database
This course runs its examples on PostgreSQL. If you use MySQL, SQL Server, Oracle or SQLite at work, you'll still be at home: the everyday core of SQL is widely shared, and lessons point out wherever another database writes something differently.
Common Mistakes
"psql: command not found" or "'psql' is not recognized." The PostgreSQL tools aren't on your terminal's path. On macOS, run the echo 'export PATH=...' line from the install steps and open a new terminal. On Windows, use SQL Shell (psql) from the Start menu, or add C:\Program Files\PostgreSQL\18\bin to your Path.
Typing SQL into the terminal instead of psql. If your terminal answers command not found: SELECT, you're talking to the shell, not to the database. Start psql first and type SQL at its prompt.
"password authentication failed for user." The password is the one you chose during installation (Windows), or your user has no password and should connect without one (macOS with Homebrew, Linux after createuser).
"connection refused" or "No such file or directory" when psql starts. The client is installed but the server isn't running. Run pg_isready to confirm, then start the server as described in Check That It Works.
Running \c or \i in a graphical client. These are psql commands, not SQL, so pgAdmin and DBeaver report a syntax error. Use the client's own menus instead, or run them in psql.
Loading the script into the wrong database. If you run \i while connected to postgres, the tables end up there. Run \c feetracker first; the prompt shows feetracker=# when you're in the right place.
Practice
Challenge: In Try it or in psql (connected to feetracker), check that the payments arrived: count the rows in the payments table. Use the counting query from Load the Fee Tracker Database and change the table name. Together with the courses count from step 4, that checks two of the five tables; the answers should match Try it.
Show solution
SELECT count(*) FROM payments; count
-------
7
(1 row)count(*) counts rows. You'll learn it properly in the lesson on aggregate functions; for now, it's a quick way to check that a table has the rows you expect.
Key Takeaways
| You want to | Do this |
|---|---|
| Run an example without installing anything | Press Try it |
| Check the client is installed | psql --version |
| Check the server is running | pg_isready |
| Connect to a local database | psql -d feetracker |
| List tables | \dt |
| Describe a table | \d students |
| Run a script file | \i /path/to/file.sql |
| Leave psql | \q |
Was this tutorial helpful?