PostgreSQL Installation

Step-by-step guide to install PostgreSQL on your system

💿 Installing PostgreSQL

PostgreSQL installation is straightforward on all major operating systems. The installer includes the database server, command-line tools, and pgAdmin 4 graphical interface for easy database management.


# Check if PostgreSQL is installed
psql --version
                                    

Output:

psql (PostgreSQL) 16.1

Installation Methods

🪟

Windows

Download installer from postgresql.org

EDB Installer Chocolatey
🍎

macOS

Use Homebrew or Postgres.app

Homebrew Postgres.app
🐧

Linux

Install via package manager

apt yum dnf
🐳

Docker

Run PostgreSQL in containers

Docker Hub Docker Compose

🔹 Windows Installation

The Windows installer provides a graphical setup wizard that guides you through the installation process. It automatically configures the database server and installs all necessary components.

Installation Steps:

  1. Download installer from postgresql.org/download/windows
  2. Run the installer as Administrator
  3. Choose installation directory (default: C:\Program Files\PostgreSQL\16)
  4. Select components: PostgreSQL Server, pgAdmin 4, Command Line Tools
  5. Set a strong password for the postgres superuser
  6. Choose port number (default: 5432)
  7. Select locale (default: your system locale)
  8. Complete installation and launch Stack Builder if needed

# After installation, test connection
psql -U postgres

# You'll be prompted for the password you set
                            

Output:

psql (16.1)
Type "help" for help.

postgres=#

🔹 macOS Installation

macOS users can install PostgreSQL using Homebrew package manager or Postgres.app. Homebrew offers command-line control while Postgres.app provides a simple menu bar application.

🔸 Using Homebrew (Recommended)


# Install Homebrew if not already 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

# Create your user database
createdb $USER
                            

🔸 Using Postgres.app

  1. Download Postgres.app from postgresapp.com
  2. Move to Applications folder
  3. Double-click to start PostgreSQL
  4. Click "Initialize" to create a new server
  5. PostgreSQL runs in the menu bar

🔹 Linux Installation

Linux distributions provide PostgreSQL through their package managers. The installation process varies slightly between distributions but follows similar patterns for setup and configuration.

🔸 Ubuntu/Debian


# Update package list
sudo apt update

# Install PostgreSQL
sudo apt install postgresql postgresql-contrib

# Start PostgreSQL service
sudo systemctl start postgresql
sudo systemctl enable postgresql

# Switch to postgres user
sudo -i -u postgres
psql
                            

🔸 CentOS/RHEL/Fedora


# Install PostgreSQL
sudo dnf install postgresql-server postgresql-contrib

# Initialize database
sudo postgresql-setup --initdb

# Start and enable service
sudo systemctl start postgresql
sudo systemctl enable postgresql
                            

🔹 Docker Installation

Docker provides a quick way to run PostgreSQL without installing it directly on your system. This method is perfect for development, testing, and creating isolated database environments.


# Pull PostgreSQL image
docker pull postgres:16

# Run PostgreSQL container
docker run --name my-postgres \
  -e POSTGRES_PASSWORD=mysecretpassword \
  -p 5432:5432 \
  -d postgres:16

# Connect to PostgreSQL
docker exec -it my-postgres psql -U postgres
                            

🔸 Using Docker Compose


# docker-compose.yml
version: '3.8'
services:
  postgres:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: mysecretpassword
      POSTGRES_DB: myapp
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
                            

🔹 Verify Installation

After installation, verify that PostgreSQL is running correctly and you can connect to it. These commands check the version and test database connectivity.


# Check PostgreSQL version
psql --version

# Check if service is running (Linux/macOS)
sudo systemctl status postgresql

# Connect to PostgreSQL
psql -U postgres

# Inside psql, check connection
SELECT version();
                            

Output:

PostgreSQL 16.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.2.0, 64-bit

🔹 Post-Installation Setup

Configure PostgreSQL for optimal security and performance. These initial settings help secure your database and prepare it for development or production use.

Important Steps:

  • Change default password: Set a strong password for postgres user
  • Configure pg_hba.conf: Control client authentication
  • Edit postgresql.conf: Adjust performance settings
  • Create databases: Set up databases for your applications
  • Create users: Add application-specific database users

-- Change postgres user password
ALTER USER postgres PASSWORD 'new_secure_password';

-- Create a new database
CREATE DATABASE myapp;

-- Create a new user
CREATE USER myuser WITH PASSWORD 'userpassword';

-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE myapp TO myuser;
                            

🧠 Test Your Knowledge

What is the default port for PostgreSQL?