PostgreSQL Fundamentals
From nothing installed to your first query, using the tool you'll actually use day to day: psql.
Beginner
pgvector) without becoming less reliable. Everything in this section after this page is PostgreSQL-specific; the SQL Fundamentals page covers what's portable to other engines.# Ubuntu/Debian
sudo apt install postgresql
# macOS (Homebrew)
brew install postgresql@16
# Connect to the default database as the postgres superuser
sudo -u postgres psql
# Or, once you have your own role/database:
psql -U myuser -d mydatabase -h localhost
psql is the official command-line client — it's what you'll use constantly, including for things a GUI makes awkward, like piping a script in from a file or a Unix pipeline.| Command | Does |
|---|---|
\l | list databases |
\c dbname | connect to a different database |
\dt | list tables in the current schema |
\d tablename | describe a table — columns, types, indexes, constraints |
\du | list roles/users |
\x | toggle expanded output — invaluable for wide rows |
\timing | show how long each query took |
\q | quit |
psql-specific, not SQL — they never work inside application code, only interactively (or in a .sql script run through psql itself).CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
done BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
INSERT INTO tasks (title) VALUES ('Write PostgreSQL fundamentals page');
SELECT * FROM tasks;
-- id | title | done | created_at
-- ----+-------------------------------------+------+-------------------------------
-- 1 | Write PostgreSQL fundamentals page | f | 2026-08-09 10:14:02.881+00
SERIAL is shorthand for an auto-incrementing integer backed by a sequence — the modern equivalent, GENERATED ALWAYS AS IDENTITY, is preferred in new schemas but you'll see SERIAL constantly in existing code.public), and each schema contains tables. Unqualified table names resolve against search_path, which defaults to public. This matters the first time you see schema.table notation and wonder what the prefix is.