Introduction to PostgreSQL

PostgreSQL (often called Postgres) is a free and open-source relational database. A relational database stores data in tables, and you talk to it using SQL (Structured Query Language). Just as people understand languages like English, relational databases like PostgreSQL, MySQL and SQL Server understand SQL.

PostgreSQL began as a university project in the 1980s. Today it stores the data behind many websites and apps.

First SQL query

Here is how a simple SQL query looks. Don't worry if it looks new. You will learn every part of it in the coming lessons.

Below is a query to read data from a table called employees.

SELECT * FROM employees;

Here is what each part of the query means:

+--------+    +---+    +------+    +-----------+    +-+
| SELECT | -> | * | -> | FROM | -> | employees | -> |;|
+--------+    +---+    +------+    +-----------+    +-+
|             |          |         |                  |
|             |          |         |                  |
Read          All        Data      Table         End of
data          columns    source    name          query

Now let's understand some basic terms.

Table:

Think of a table like a spreadsheet. It stores information in rows and columns. Each row is one record, and each column is one kind of information. For example, the employees table stores each employee's id, name and department.

+---------------------------+
|        employees          |    <--------- Table name
+----+--------+-------------+
| id | name   | department  |    <--------- Columns
+----+--------+-------------+    <-
| 1  | Alice  | HR          |      |
| 2  | Bob    | IT          |      |
| 3  | John   | Sales       |      |------- Rows
| 4  | David  | Marketing   |      |
| 5  | Emma   | IT          |      |
+----+--------+-------------+    <-

Database:

A database is a group of tables that belong together. For example, a company might have a database with tables for employees, departments and salaries.

+---------------------------+
|        company_db         |
+---------------------------+
| (employees)   (salaries)  |
| (departments) (leaves)    |
| (projects)    (...)       |
+---------------------------+

RDBMS:

An RDBMS (Relational Database Management System) is the software that stores your databases and runs your SQL. PostgreSQL is an RDBMS. Others include MySQL, Oracle and SQL Server.

psql:

In this course, your queries run in psql, the PostgreSQL command-line tool. psql runs each query and prints the result as a table. You will see what it prints in the next lesson.


The first exercise is already written for you. Just hit Submit to run your first query.