Installing PostgreSQL
Learning Objectives
After this lesson, you will:
- Install PostgreSQL from package repository
- Understand how to install PostgreSQL from source (optional)
- Configure basic
postgresql.conffor HA - Understand
pg_hba.confand authentication - Prepare PostgreSQL on 3 nodes for Patroni cluster
1. Installing PostgreSQL from Package Repository
1.1. Preparation
Before installing PostgreSQL, we need to setup the official PostgreSQL package repository (PGDG - PostgreSQL Global Development Group).
Advantages of PGDG repository:
- ✅ Latest PostgreSQL versions
- ✅ Fast security updates
- ✅ Many extensions available
- ✅ Support for multiple distros
1.2. Installation on Ubuntu/Debian
Step 1: Add PGDG repository
Step 2: Install PostgreSQL
Step 3: Check service
Step 4: Stop and disable PostgreSQL default cluster
1.3. Installation on CentOS/RHEL/Rocky Linux
Step 1: Add PGDG repository
Step 2: Install PostgreSQL
Step 3: Create symlink (optional but convenient)
Step 4: Do not initialize database (Patroni will do it)
2. Installing PostgreSQL from Source (Optional - Advanced)
Installing from source allows custom compile options, but is more complex and harder to maintain.
2.1. When to install from source?
- 🔧 Need custom features not available in binary packages
- 🔧 Testing with development version
- 🔧 Optimization for specific hardware
- 🔧 Apply custom patches
2.2. Process for installing from source
Step 1: Install dependencies
Step 2: Download source
Step 3: Configure and compile
Step 4: Setup environment
Note: Installing from source does not automatically include systemd service, needs to be created manually.
3. Basic postgresql.conf Configuration
Patroni will manage most PostgreSQL configurations through DCS. However, it's important to understand the key parameters.
3.1. postgresql.conf file structure
3.2. Important parameters for HA
Replication Settings
Memory Settings
Checkpoint Settings
Logging Settings
3.3. Patroni will override these settings
Patroni manages the following parameters through DCS, DO NOT set in postgresql.conf:
Patroni will automatically set them in postgresql.auto.conf.
4. Understanding pg_hba.conf
pg_hba.conf (Host-Based Authentication) controls client authentication.
4.1. Structure of pg_hba.conf
4.2. The columns
- TYPE:
local: Unix socket connectionshost: TCP/IP (clear text or SSL)hostssl: TCP/IP with SSL onlyhostnossl: TCP/IP without SSL
- DATABASE:
- Database name
all: all databasesreplication: replication connections
- USER:
- Username
all: all users
- ADDRESS:
- IP/netmask:
10.0.1.0/24 - Hostname
0.0.0.0/0: anywhere (not recommended)
- IP/netmask:
- METHOD:
trust: No password required (only for local dev)md5: MD5 hashed password (deprecated)scram-sha-256: Modern, secure (recommended)peer: Unix username = PostgreSQL usernamecert: SSL certificate authentication
4.3. pg_hba.conf cho Patroni Cluster
4.4. Best practices for pg_hba.conf
✅ Specific is better: Don't use 0.0.0.0/0 if not needed
✅ Use scram-sha-256: Modern authentication method
✅ Separate users: Different users for app, replication, monitoring
✅ Document: Comment for each rule
✅ Restrict replication: Only allow replication user from Patroni nodes IP
❌ Avoid trust method: Even in dev environment
5. Create necessary users and databases
5.1. Create replication user
5.2. Create Patroni monitoring user
5.3. Create application database and user
6. Lab: Installing PostgreSQL on 3 nodes
6.1. Lab Environment
6.2. Execute on ALL 3 nodes
Step 1: Update system
Step 2: Install PostgreSQL 18
Step 3: Stop and disable default cluster
Step 4: Create directories for PostgreSQL data
Step 5: Check PostgreSQL binary
6.3. Verify on each node
6.4. Troubleshooting
Issue 1: Permission denied trên data directory
Issue 2: PostgreSQL service still running
Issue 3: Port 5432 is already in use
7. Summary
Key Takeaways
✅ Package repository: Install PostgreSQL from PGDG repo to get the latest version
✅ Disable default service: Patroni will manage PostgreSQL, don't use default systemd service
✅ postgresql.conf: Understand important parameters for HA and replication
✅ pg_hba.conf: Configure authentication for connections and replication
✅ Don't initialize cluster: Patroni will bootstrap cluster automatically
Lab Checklist
- PostgreSQL 18 installed on all 3 nodes
- Default cluster removed
- PostgreSQL service disabled
- Data directories created with correct permissions
- Binary paths available in $PATH
Preparation for Lesson 6
The next lesson will install and configure etcd cluster - DCS layer for Patroni.