Installation
Use this lesson to understand Installation with practical steps and examples for every major platform.
Docker Installation (Recommended for All Platforms)
Docker provides the most repeatable and isolated environment for learning. You can destroy and recreate it in seconds.
Quick Start
docker run --name pg-lab \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
-d postgres:16
Connect
psql -h localhost -U postgres -d postgres
With Persistent Storage
docker run --name pg-lab \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=pg_lab \
-p 5432:5432 \
-v pg_data:/var/lib/postgresql/data \
-d postgres:16
Docker Compose (For Richer Setups)
# docker-compose.yml
services:
postgres:
image: postgres:16
container_name: pg-lab
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: pg_lab
ports:
- "5432:5432"
volumes:
- pg_data:/var/lib/postgresql/data
volumes:
pg_data:
docker compose up -d
Windows Installation
Method 1: EDB Installer (Recommended)
-
Download the installer:
- Visit https://www.postgresql.org/download/windows/
- Choose the EDB installer for PostgreSQL 16+
-
Run the installer:
- Select components: PostgreSQL Server, pgAdmin 4, Command Line Tools
- Set installation directory (default:
C:\Program Files\PostgreSQL\16) - Set data directory
- Set superuser (
postgres) password - Default port:
5432 - Default locale: your system locale
-
Verify installation:
psql --version- Should return:
psql (PostgreSQL) 16.x
- Should return:
-
Connect:
psql -U postgres
Method 2: Chocolatey (Package Manager)
choco install postgresql16
Method 3: Scoop
scoop install postgresql
macOS Installation
Method 1: Homebrew (Recommended)
-
Install Homebrew (if not installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" -
Install PostgreSQL:
brew install postgresql@16 -
Start PostgreSQL service:
brew services start postgresql@16 -
Verify:
psql --version
psql -U $(whoami) -d postgres
Method 2: Postgres.app (GUI-Based)
-
Download from https://postgresapp.com/
-
Drag to Applications folder
-
Click "Initialize" to create a default cluster
-
Add CLI tools to PATH:
echo 'export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
Linux Installation
Ubuntu/Debian
-
Add the official PostgreSQL APT repository (for latest version):
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -
Install PostgreSQL:
sudo apt update
sudo apt install -y postgresql-16 postgresql-client-16 -
Check status:
sudo systemctl status postgresql -
Connect:
sudo -u postgres psql
CentOS / RHEL / Rocky Linux
-
Add PostgreSQL repository:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm -
Disable built-in PostgreSQL module:
sudo dnf -qy module disable postgresql -
Install PostgreSQL:
sudo dnf install -y postgresql16-server postgresql16 -
Initialize and start:
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
sudo systemctl start postgresql-16
sudo systemctl enable postgresql-16 -
Connect:
sudo -u postgres psql
Arch Linux
sudo pacman -S postgresql
sudo -u postgres initdb -D /var/lib/postgres/data
sudo systemctl start postgresql
sudo systemctl enable postgresql
Post-Installation Setup for All Systems
-
Connect to PostgreSQL:
sudo -u postgres psql # Linux
psql -U postgres # Docker / Windows / macOS -
Set or change the postgres password:
ALTER USER postgres WITH PASSWORD 'NewSecurePassword123!'; -
Create a lab database:
CREATE DATABASE pg_lab;
\c pg_lab
SELECT version(); -
Create a test table and insert data:
CREATE TABLE users (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL
);
INSERT INTO users (name) VALUES ('Test User');
SELECT * FROM users;
Installing Client Tools
pgAdmin 4 (Official GUI)
Windows/macOS:
- Included with EDB installer, or download at https://www.pgadmin.org/download/
Linux (Ubuntu/Debian):
curl -fsS https://www.pgadmin.org/static/packages_pgdg/pgadmin4.pub | sudo gpg --dearmor -o /usr/share/keyrings/pgadmin4.gpg
echo "deb [signed-by=/usr/share/keyrings/pgadmin4.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" | sudo tee /etc/apt/sources.list.d/pgadmin4.list
sudo apt update
sudo apt install -y pgadmin4-desktop
DBeaver (Universal Database Tool)
# Ubuntu/Debian
sudo snap install dbeaver-ce
# macOS
brew install --cask dbeaver-community
DataGrip (JetBrains IDE)
- Download from https://www.jetbrains.com/datagrip/
- Requires license (free for students/educators)
Common Installation Issues
Windows:
psql: could not connect to server: Connection refused- Check that the PostgreSQL service is running (Services →
postgresql-x64-16)
- Check that the PostgreSQL service is running (Services →
macOS:
command not found: psql- Add to PATH:
echo 'export PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH"' >> ~/.zshrc
- Add to PATH:
Linux:
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed- Check that the service is running:
sudo systemctl status postgresql - Verify the socket directory exists:
ls /var/run/postgresql/
- Check that the service is running:
Docker:
connection refusedwhen connecting tolocalhost:5432- Ensure you published the port:
-p 5432:5432 - Wait a few seconds after starting the container
- Ensure you published the port:
Configuration Files Location
| OS | Data Directory | Main Config |
|---|---|---|
| Ubuntu/Debian | /var/lib/postgresql/16/main/ | /etc/postgresql/16/main/postgresql.conf |
| CentOS/RHEL | /var/lib/pgsql/16/data/ | /var/lib/pgsql/16/data/postgresql.conf |
| macOS (Homebrew) | /opt/homebrew/var/postgresql@16/ | Same directory |
| Windows | C:\Program Files\PostgreSQL\16\data\ | Same directory |
| Docker | /var/lib/postgresql/data/ | Same directory |
Uninstallation Guides
Windows:
- Control Panel → Uninstall Programs → PostgreSQL 16
- Delete data directory:
C:\Program Files\PostgreSQL\16
macOS:
brew services stop postgresql@16
brew uninstall postgresql@16
rm -rf /opt/homebrew/var/postgresql@16
Ubuntu/Debian:
sudo apt purge postgresql-16 postgresql-client-16
sudo rm -rf /etc/postgresql /var/lib/postgresql
Docker:
docker stop pg-lab && docker rm pg-lab
docker volume rm pg_data # if using named volume
Next Steps After Installation
-
Create a non-superuser role:
CREATE ROLE app_user LOGIN PASSWORD 'SecurePass123!';
GRANT CONNECT ON DATABASE pg_lab TO app_user;
GRANT USAGE ON SCHEMA public TO app_user;
GRANT ALL ON ALL TABLES IN SCHEMA public TO app_user; -
Test remote connection (if needed):
# Edit pg_hba.conf to allow remote connections
# Add: host all all 0.0.0.0/0 scram-sha-256
# Then reload:
sudo systemctl reload postgresql -
Configure firewall:
sudo ufw allow 5432/tcp # Linux
Version Check & Updates
Check installed version:
SELECT version();
Or from the command line:
psql --version
pg_config --version
Update PostgreSQL (Ubuntu example):
sudo apt update && sudo apt upgrade postgresql-16
You're now ready to start working with PostgreSQL!
Concept Map
flowchart LR
A[Choose Platform] --> B[Install PostgreSQL]
B --> C[Start Service]
C --> D[Connect with psql]
D --> E[Create Lab Database]
E --> F[Create Test Tables]
Common Pitfalls
| Pitfall | Consequence | Prevention |
|---|---|---|
Not installing postgresql-client separately | Can't connect from other machines | Install postgresql-client-16 for remote psql access |
| Forgetting Docker port mapping | connection refused on localhost | Always use -p 5432:5432 |
Using default postgres superuser for applications | High blast radius on mistakes | Create dedicated roles with least privilege |
| Not securing the default installation | Open to local connections | Run through pg_hba.conf settings and set strong passwords |
Quick Reference
# Check client and server availability
psql --version
sudo systemctl status postgresql # Linux
brew services list # macOS
# Connect to PostgreSQL
sudo -u postgres psql # Linux (peer auth)
psql -h localhost -U postgres # Docker / password auth
What's Next
- Next: psql and Client Tools — Continue to the next concept with incremental complexity.
- Section Overview — Return to this section index and choose another related lesson.