PostgreSQL Tutorial menu

PostgreSQL Introduction

PostgreSQL is an advanced, free and open-source relational database system.

What is PostgreSQL?

  • PostgreSQL is a relational database management system
  • It stores data in tables and you work with it using SQL
  • It is open source, with a very permissive licence: free for any project
  • It began at the University of California, Berkeley, in 1986, and has been developed by a worldwide community ever since
  • New major versions come out every year

You will see it written as PostgreSQL, Postgres or PG. They all mean the same thing.

What Makes PostgreSQL Special?

FeatureWhat it gives you
StandardsFollows the SQL standard closely
Strict dataRefuses wrong data instead of silently changing it
JSONBStore and search JSON documents inside a table
ArraysA column can hold a list, like {'Urdu','English'}
RETURNINGGet back the rows you just inserted or changed
ExtensionsAdd features such as PostGIS (maps) or pgvector (AI search)
ConcurrencyMany users can read and write at the same time without blocking each other

PostgreSQL's strictness is a good thing. If you try to put 'abc' into a number column, or a date like 2026-02-30, it stops you with an error, so bad data never gets in.

Where is PostgreSQL Used?

  • Web apps built with Django, Laravel, Ruby on Rails, Node.js and Next.js
  • Cloud services such as Supabase, Neon, Amazon RDS and Google Cloud SQL
  • Banking, maps (with PostGIS), analytics and AI apps (with pgvector)
  • Companies such as Instagram, Spotify and Reddit have used it

The Sample Database

This tutorial uses a small database called institute, with the same data as the SQL tutorial.

Example

sql
SELECT * FROM students;
Output
 id |       name      |    city   | age | marks | course_id
----+-----------------+-----------+-----+-------+-----------
  1 | Ali Khan        | Karachi   |  19 |    78 |         1
  2 | Ayesha Siddiqui | Lahore    |  21 |    85 |         2
  3 | Bilal Ahmed     | Karachi   |  22 |    64 |         1
  4 | Fatima Noor     | Islamabad |  20 |    92 |         3
  5 | Hamza Raza      | Karachi   |  18 |    70 |
  6 | Sana Iqbal      | Lahore    |  23 |    88 |         2
(6 rows)

In the psql client, NULL is shown as an empty space, as in Hamza's course_id.

Example

sql
SELECT * FROM courses;
Output
 id |       title        | duration_months
----+--------------------+-----------------
  1 | Web Development    |               6
  2 | Python Programming |               4
  3 | Graphic Designing  |               3
  4 | Digital Marketing  |               3
(4 rows)

students.course_id points to courses.id.

PostgreSQL and MySQL

Both are excellent free relational databases, and basic SQL is the same in both. A few differences:

PostgreSQLMySQL
Auto numbersGENERATED ALWAYS AS IDENTITY or SERIALAUTO_INCREMENT
Join textThe double-pipe operator or CONCATCONCAT
Case-insensitive searchILIKELIKE (usually already ignores case)
JSONJSON and the faster JSONBJSON
Get changed rows backRETURNINGNot available

Exercise

Exercise

What does PostgreSQL do if you try to store the text 'abc' in an INTEGER column?

Learn PostgreSQL with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp