-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule1
More file actions
291 lines (200 loc) · 8.87 KB
/
module1
File metadata and controls
291 lines (200 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
---
# 🧩 **Module 1: Introduction to PostgreSQL**
---
## **1. What is PostgreSQL?**
### **1.1 Overview of PostgreSQL**
PostgreSQL (pronounced *Post-Gres-Q-L*) is an **advanced open-source Object-Relational Database Management System (ORDBMS)** developed at the University of California, Berkeley.
It is one of the most **powerful, stable, and feature-rich** database systems used worldwide in production-grade environments.
Key points:
* Written in **C language** for performance and flexibility.
* Follows the **SQL standard (SQL:2011)** with full ACID compliance.
* Can handle both **OLTP (Online Transaction Processing)** and **OLAP (Analytical)** workloads.
* Supports both **relational** and **non-relational** (JSON, key-value) data models.
* Strong reputation for **data integrity**, **stability**, and **extensibility**.
---
### **1.2 History of PostgreSQL**
* **1986** – Originated as the POSTGRES project at UC Berkeley led by Prof. Michael Stonebraker.
* **1994** – SQL support added, renamed **Postgres95**.
* **1996** – Became **PostgreSQL** and open-sourced under a permissive license.
* **Today** – Managed by a global community with continuous enhancements, currently at **PostgreSQL 17 (as of 2025)**.
---
### **1.3 PostgreSQL Architecture Overview**
PostgreSQL architecture consists of several key components:
* **Postmaster (Server Process)** – Main process that manages client connections and background workers.
* **Shared Memory** – Stores buffer cache, locks, and transaction logs for all sessions.
* **Background Processes:**
* **WAL Writer:** Handles write-ahead logs.
* **Background Writer:** Writes dirty buffers to disk.
* **Autovacuum Daemon:** Cleans up dead tuples to maintain performance.
* **Client Process:** Users connect through clients like `psql`, `pgAdmin`, or applications (Python, Java, etc.).
* **Storage System:** Organizes data in files and directories under the data directory (`PGDATA`).
**Simplified Architecture Diagram:**
```
+------------------------+
| Applications |
| (pgAdmin, psql, APIs) |
+----------+-------------+
|
v
+------------------------+
| PostgreSQL Server (Postmaster)
| - Connection Manager
| - Query Processor
| - Executor
| - Background Workers
+------------------------+
|
v
+------------------------+
| Storage & WAL Logs |
| - Data Files |
| - Write Ahead Log |
| - Buffer Cache |
+------------------------+
```
---
## **2. PostgreSQL vs Other Databases**
PostgreSQL competes with several popular database systems.
Below is a comparative overview highlighting its strengths:
| Feature | PostgreSQL | MySQL | Oracle | SQL Server |
| --------------------- | ----------------------------------- | ----------------------------- | ----------- | ----------- |
| **License** | Open Source (PostgreSQL License) | Open Source (GPL) | Proprietary | Proprietary |
| **ACID Compliance** | Full | Partial (depends on engine) | Full | Full |
| **Performance** | Optimized for complex queries | Fast for read-heavy workloads | Excellent | Excellent |
| **JSON Support** | Native JSON + JSONB | Basic JSON | Yes | Yes |
| **Extensibility** | High – user-defined types/functions | Limited | Moderate | Moderate |
| **Indexing Options** | B-Tree, Hash, GIN, GiST, BRIN | B-Tree | B-Tree | B-Tree |
| **Stored Procedures** | PL/pgSQL, Python, C, etc. | SQL/PSM | PL/SQL | T-SQL |
| **Replication** | Built-in streaming, logical | Basic (InnoDB) | Advanced | Advanced |
| **Best Use Case** | Enterprise, analytics, GIS | Web apps | Enterprise | Enterprise |
**Conclusion:**
PostgreSQL stands out for its **standards compliance**, **advanced indexing**, **open-source freedom**, and **extensibility**, making it a top choice for enterprise and cloud-native systems.
---
## **3. Features and Benefits of PostgreSQL**
PostgreSQL offers a rich feature set, making it suitable for small startups to enterprise-level systems.
### **3.1 Key Features**
1. **Open Source & Free**
* Completely free to use with no licensing restrictions.
* PostgreSQL License allows modification and redistribution.
2. **ACID Compliance**
* Ensures reliable transaction processing:
* **Atomicity** – All or nothing
* **Consistency** – Maintains database validity
* **Isolation** – Transactions run independently
* **Durability** – Data is permanently saved
3. **MVCC (Multi-Version Concurrency Control)**
* Enables multiple users to access data simultaneously without locks.
* Provides high concurrency and consistent reads.
4. **Extensibility**
* Create your own **data types**, **operators**, and **functions**.
* Example:
```sql
CREATE FUNCTION add_three(x int) RETURNS int AS $$
BEGIN
RETURN x + 3;
END;
$$ LANGUAGE plpgsql;
```
5. **Advanced Data Types**
* Supports JSON, arrays, XML, UUID, hstore (key-value store), geometric, and full-text search.
6. **Robust Security**
* Role-based access control, SSL encryption, row-level security, and LDAP/Kerberos integration.
7. **Replication & High Availability**
* Built-in **streaming replication**, **logical replication**, and **point-in-time recovery (PITR)**.
8. **Full-Text Search**
* Powerful search capabilities for textual data using `tsvector` and `tsquery`.
9. **Cross-Platform & Cloud Ready**
* Works on Linux, macOS, Windows, and is supported by major cloud providers (AWS RDS, Azure Database, GCP Cloud SQL).
---
### **3.2 Benefits of PostgreSQL**
| Category | Benefit |
| --------------------- | ------------------------------------------------------ |
| **Performance** | Optimized for large datasets and complex queries |
| **Reliability** | Stable with automatic crash recovery |
| **Scalability** | Handles terabytes of data and thousands of connections |
| **Extensibility** | Add new features without changing core code |
| **Community Support** | Global, active open-source community |
| **Integration** | Compatible with Python, Java, Node.js, R, etc. |
---
## **4. Setting Up PostgreSQL**
### **4.1 Installation on Different Platforms**
#### **Linux (Ubuntu/Debian)**
```bash
sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl enable postgresql
sudo systemctl start postgresql
```
#### **Windows**
1. Download installer: [https://www.postgresql.org/download/windows/](https://www.postgresql.org/download/windows/)
2. Run setup wizard → select components (PostgreSQL Server, pgAdmin).
3. Set password for “postgres” user and note the port (default 5432).
4. Access pgAdmin or `psql`.
#### **macOS (Homebrew)**
```bash
brew update
brew install postgresql
brew services start postgresql
```
---
### **4.2 Basic Configuration and Tools**
#### **PostgreSQL Tools**
1. **psql** – Command-line tool for executing SQL.
2. **pgAdmin** – GUI-based management tool.
3. **pg_dump / pg_restore** – For backups and restores.
4. **pg_stat_activity** – For monitoring sessions.
#### **Config Files (Linux path: `/etc/postgresql/<version>/main/`)**
* `postgresql.conf` – Server configuration (port, memory, etc.)
* `pg_hba.conf` – Authentication and access control.
* `pg_ident.conf` – Maps OS users to DB roles.
---
### **4.3 Connecting to PostgreSQL**
#### **Using psql**
```bash
sudo -u postgres psql
```
or
```bash
psql -h localhost -U postgres -d postgres
```
#### **Using pgAdmin**
* Open pgAdmin → Add New Server.
* Fill in connection info:
* **Host:** localhost
* **Port:** 5432
* **Username:** postgres
* **Password:** your_password
Once connected, you can create databases, run SQL queries, and manage users easily.
---
### **4.4 Accessing PostgreSQL via Docker**
#### **Run PostgreSQL in Docker**
```bash
docker run --name postgres-demo \
-e POSTGRES_PASSWORD=admin123 \
-p 5432:5432 \
-d postgres
```
#### **Run pgAdmin in Docker**
```bash
docker run --name pgadmin4 \
-p 8080:80 \
-e 'PGADMIN_DEFAULT_EMAIL=admin@demo.com' \
-e 'PGADMIN_DEFAULT_PASSWORD=admin123' \
-d dpage/pgadmin4
```
Access pgAdmin at `http://localhost:8080` → Connect to PostgreSQL container using:
* Host: `host.docker.internal` (Windows/macOS) or `172.17.0.1` (Linux)
* Port: `5432`
---
### **4.5 Verifying Installation**
```bash
psql --version
```
```bash
SELECT version();
```
Output example:
```
PostgreSQL 17.1 (Ubuntu 17.1-1)
```
✅ **End of Module 1: Introduction to PostgreSQL**