BeginnerPostgreSQL · Lesson 1 of 7

Setting up PostgreSQL & psql

Install PostgreSQL, connect with psql and create your first database.

PostgreSQL ("Postgres") is a free, open-source relational database used by companies of every size. Data lives in tables of rows and columns, and you talk to it with SQL (Structured Query Language).

Install it from postgresql.org (Windows/macOS installers), with your Linux package manager, or run it in Docker. Managed services such as Neon, Supabase, AWS RDS and Railway host it for you.

psql is the command-line client. SQL statements end with a semicolon; commands starting with a backslash are psql helpers: \l lists databases, \c connects to one, \dt lists tables, \d table describes a table and \q quits.

TerminalShell
# Option A: Docker (works on any OS)
docker run --name pg -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres:16

# Option B: Ubuntu / Debian
sudo apt install postgresql

# Connect and create the course database
psql -h localhost -U postgres
psqlSQL
CREATE DATABASE school;
\c school
SELECT version();
SELECT now(), current_user, current_database();
\l
\q

Key points

  • PostgreSQL stores data in tables; SQL is how you read and change it.
  • SQL statements end with ; — psql waits until it sees one.
  • Backslash commands (\dt, \d, \c, \q) are psql shortcuts, not SQL.

Exercise

Install PostgreSQL, create the school database and connect to it. Use SELECT 2 + 3 * 4;, SELECT upper('dolese'); and SELECT current_date + 30; to try SQL as a calculator.

Show solution

Try the exercise yourself first — then compare your approach with this one.

SQL follows normal operator precedence (multiplication before addition). upper is a built-in text function, and adding a whole number to a date adds that many days.

psqlSQL
SELECT 2 + 3 * 4;            -- 14
SELECT upper('dolese');      -- DOLESE
SELECT current_date + 30;    -- the date 30 days from today
SELECT current_date + 30 AS due_date, to_char(current_date + 30, 'Day DD Mon YYYY') AS readable;

Check your understanding

  1. Which symbol ends an SQL statement in psql?

  2. What does the psql command \dt do?

  3. How do you switch to the school database inside psql?

  4. Which statement creates a new database?

Ask AI