PostgreSQL Basics
1. What is PostgreSQL?
PostgreSQL is an open-source, object-relational database management system (ORDBMS). It
is known for reliability, ACID compliance, and advanced SQL features. It supports both SQL
(relational) and JSON (non-relational) querying.
2. Key Features
Cross-platform: Works on Windows, Linux, macOS
ACID compliant: Ensures safe transactions
Advanced indexing: B-Tree, Hash, GIN, GiST, BRIN
Full-text search support
Extensibility: Define custom data types, operators, and functions
MVCC (Multi-Version Concurrency Control) for high-performance concurrent
operations
3. Common Data Types
Numeric: INTEGER, SERIAL, BIGINT, NUMERIC, REAL
Text: CHAR, VARCHAR, TEXT
Date/Time: DATE, TIME, TIMESTAMP
Boolean: BOOLEAN
JSON/Array: JSON, JSONB, ARRAY
4. Basic Commands
-- Create a Database
CREATE DATABASE college_db;
-- Connect to a Database
\c college_db;
-- Create a Table
CREATE TABLE students (
student_id SERIAL PRIMARY KEY,
name VARCHAR(50),
age INT,
department VARCHAR(50)
);
-- Insert Data
INSERT INTO students (name, age, department)
VALUES ('John Doe', 20, 'Computer Science');
-- Select Data
SELECT * FROM students;
-- Update Data
UPDATE students SET age = 21 WHERE student_id = 1;
-- Delete Data
DELETE FROM students WHERE student_id = 1;
5. PostgreSQL Tools
psql – Command-line interface
pgAdmin – GUI for managing PostgreSQL
libpq – C library for connecting applications
6. Advantages
Open-source and free
Rich set of functions
High security with role-based access
Large community and strong documentation