Setting Up SQL

Updated · 14 min read
easyPostgreSQL 18sqlpostgresqlsetuppsql

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.

Try it in the browser, or PostgreSQL on your own computer

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 --version and pg_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.sql script 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.

The real Try it editor, after running the version query

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:

SQL▶ Try it
SHOW server_version;
Output
 server_version
----------------
 18.7
(1 row)

Three things are worth knowing about Try it:

FactWhat it means for you
It's a real databaseErrors, results and behavior are exactly what PostgreSQL does on a server
It runs in your browserThe 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 freshEach 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.

Shell
# 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 ~/.zshrc

On Linux there are two ways to install, and they give different versions:

WayWhat you getWhen to use it
Your distribution's package (the steps above)Whatever release your Ubuntu or Debian version ships, which may be older than 18Fine for the first levels of this course; everything here works on any recent release
The PostgreSQL Apt RepositoryPostgreSQL 18 itself, kept up to date by the PostgreSQL projectWhen 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):

Shell
psql --version
pg_isready
CommandWhat it checksA good answer looks like
psql --versionThe client is installed and on your pathpsql (PostgreSQL) 18. and an update number
pg_isreadyThe server is running and accepting connectionsA line ending in accepting connections
psql --version checks the client; pg_isready checks the server

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:

Shell
psql -d postgres

The prompt shows the database you're connected to, followed by =# or =>:

text
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:

CommandWhat it does
\lList all databases
\c feetrackerConnect to the database named feetracker
\dtList the tables in the current database
\d studentsDescribe the students table: its columns, types and keys
\i fee-tracker.sqlRun every statement in a file
\?Show help for all backslash commands
\qQuit psql
SQL statements end with a semicolon; psql commands start with a backslash

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.

  1. Download fee-tracker.sql and note the folder it's saved in.
  2. 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:
text
postgres=# CREATE DATABASE feetracker;
CREATE DATABASE
postgres=# \c feetracker
You are now connected to database "feetracker" as user "you".
feetracker=#
  1. Run the script with \i, giving the full path to the file. Use forward slashes, even on Windows, and replace you and the folder with your own user name and download location:
text
\i /Users/you/Downloads/fee-tracker.sql

psql 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.

Download, create and connect, run the script, check the rows
  1. Check that everything arrived. This query counts the rows in the courses table; your local database should show the same answer as Try it. (count(*) is SQL, so this one also works in a graphical client.)
SQL▶ Try it
SELECT count(*) FROM courses;
Output
 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.

ClientCostNotes
pgAdminFreeMade by the PostgreSQL community; installed with PostgreSQL on Windows
DBeaver CommunityFreeWorks with PostgreSQL, MySQL, SQL Server, Oracle, SQLite and more
A code editor extensionFreeEditors 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
SQL▶ Try it
SELECT count(*) FROM payments;
Output
 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.

Quick check: Setting up SQL

3 questions
1What happens to your changes when a Try it run ends?
2In psql, you typed a query and pressed Enter, and the prompt changed to postgres-#. What happened?
3Which psql command runs every statement in a file?

Key Takeaways

You want toDo this
Run an example without installing anythingPress Try it
Check the client is installedpsql --version
Check the server is runningpg_isready
Connect to a local databasepsql -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?

Lesson 4 of 115