dev.rean.me
database

Introduction to PostgreSQL Database

Learn what is PostgreSQL, its features, and how to create database, table, and query data in very simple English, with student example and easy tips for beginner.

2026-10-24 ยท 5 min read

Share:

Hello my friend! ๐Ÿ˜ Today we learn about PostgreSQL. Many developer call it "Postgres". It is one of most powerful database in the world. Let's go!

1. What Is PostgreSQL?

PostgreSQL is a RDBMS (Relational Database Management System). It keep data in table with row and column, and we use SQL to manage data.

PostgreSQL is open-source and free. It start long time ago (1986 at University of California, Berkeley), so it is very stable and trusted.

Big company like Apple, Instagram, Spotify, and Reddit use PostgreSQL.

2. Why Learn PostgreSQL?

  • โœ… Free and open-source: No need pay money, use for any project.
  • โœ… Follow SQL standard very close: What you learn here, can use in other database too.
  • โœ… Very reliable: Strong transaction support (ACID), data always safe and correct.
  • โœ… Many data type: Not only number and text, also JSON, ARRAY, UUID, date and time, and more.
  • โœ… Can extend: Add extension like PostGIS (for map and location).
  • โœ… Work with many language: Python, Java, Node.js, PHP, Go, and more.

3. PostgreSQL vs MySQL โ€” Quick Look

PostgreSQLMySQL
TypeObject-relational databaseRelational database
SQL standardFollow very closeFollow, but some difference
Data typeVery many (JSON, ARRAY, UUID...)Common type
Best forComplex query, big and serious appSimple and fast website
Auto numberSERIAL or GENERATED ... AS IDENTITYAUTO_INCREMENT

Both is very good. Choose by your project.

4. Connect with psql

psql is command-line tool come with PostgreSQL. Open terminal and type:

psql -U postgres

Some useful psql command (start with \):

CommandMeaning
\lShow all database
\c schoolConnect to school database
\dtShow all table
\d studentsShow structure of students table
\qExit psql

If you don't like command line, you can use pgAdmin โ€” it is free tool with screen and button.

5. Create a Database

CREATE DATABASE school;

Then connect to it:

\c school

Note: PostgreSQL don't have USE school; like MySQL. Use \c instead.

6. Create a Student Table

CREATE TABLE students (
  id SERIAL PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  age INT,
  email VARCHAR(100) UNIQUE,
  skills TEXT[],
  created_at TIMESTAMP DEFAULT NOW()
);
  • SERIAL mean PostgreSQL give auto number for id (1, 2, 3...).
  • UNIQUE mean two student cannot have same email.
  • TEXT[] is array โ€” one column can keep many value. MySQL don't have this!
  • DEFAULT NOW() put current date and time auto.

7. Insert Data

INSERT INTO students (name, age, email, skills)
VALUES ('Sok', 20, 'sok@example.com', ARRAY['SQL', 'Python']),
       ('Dara', 19, 'dara@example.com', ARRAY['HTML', 'CSS']),
       ('Vuth', 21, 'vuth@example.com', ARRAY['SQL', 'Java']);

Cool trick: use RETURNING to see the row you just insert:

INSERT INTO students (name, age, email)
VALUES ('Sreyna', 18, 'sreyna@example.com')
RETURNING id, name;

8. Query Data

See all student:

SELECT * FROM students;

Result:

idnameageemailskills
1Sok20sok@example.com{SQL,Python}
2Dara19dara@example.com{HTML,CSS}
3Vuth21vuth@example.com{SQL,Java}
4Sreyna18sreyna@example.comNULL

Find student older than 19:

SELECT name, age FROM students WHERE age > 19;

Find student who know SQL (search inside array):

SELECT name FROM students WHERE 'SQL' = ANY(skills);

Search name without care upper or lower case, use ILIKE:

SELECT * FROM students WHERE name ILIKE 's%';

This find Sok and Sreyna.

9. Update and Delete

UPDATE students SET age = 22 WHERE name = 'Vuth';

DELETE FROM students WHERE name = 'Dara';

10. Bonus: JSON in PostgreSQL

PostgreSQL can keep JSON data inside table, so it is SQL and a little bit like NoSQL too!

CREATE TABLE profiles (
  id SERIAL PRIMARY KEY,
  info JSONB
);

INSERT INTO profiles (info)
VALUES ('{"name": "Sok", "city": "Phnom Penh"}');

SELECT info->>'name' AS name FROM profiles
WHERE info->>'city' = 'Phnom Penh';

JSONB is JSON that store in binary, faster for search. ->> get value as text.

Tips

  • Always use single quote 'text' for text value. Double quote "name" is for column or table name in PostgreSQL.
  • PostgreSQL change table and column name to small letter, if you not use double quote. So just use small letter with underscore, like created_at.
  • Use \d table_name in psql when you forget structure of table. Very helpful.
  • Use RETURNING after INSERT, UPDATE, or DELETE to see what change.
  • Before UPDATE or DELETE, run SELECT with same WHERE first. Make sure you change the right row!
  • Use JSONB not JSON, it is faster for search.
  • Try free online PostgreSQL if you don't want install yet, then install later when ready.

Conclusion

PostgreSQL is free, powerful, and very reliable database. It follow SQL standard, have many data type like array and JSON, and big company trust it. If you know MySQL, learn PostgreSQL is easy โ€” most SQL is same. Keep learning my friend! ๐Ÿ’ก