#!/bin/bash set -euo pipefail echo "============================================" echo "PostgreSQL Installation Script" echo "============================================" echo "" echo "This script will run the following commands with sudo:" echo " - mkdir -p /etc/apt/keyrings" echo " - curl ... | gpg --dearmor -o /etc/apt/keyrings/postgresql.gpg" echo " - tee /etc/apt/sources.list.d/pgdg.list" echo " - apt-get update" echo " - apt-get install -y postgresql" echo "" read -p "Continue with installation? (y/N) " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Installation cancelled." exit 0 fi echo "" echo "Starting installation (commands will be shown as they run)..." echo "" # Enable command tracing set -x # Create directory for keyrings if it doesn't exist sudo mkdir -p /etc/apt/keyrings # Download and install the PostgreSQL GPG key if [ ! -f /etc/apt/keyrings/postgresql.gpg ]; then curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | \ sudo gpg --dearmor -o /etc/apt/keyrings/postgresql.gpg fi # Add PostgreSQL repository echo "deb [signed-by=/etc/apt/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | \ sudo tee /etc/apt/sources.list.d/pgdg.list > /dev/null # Update package lists sudo apt-get update # Install latest PostgreSQL sudo apt-get install -y postgresql # Disable command tracing for cleaner output set +x echo "" echo "============================================" # Verify installation if command -v psql &> /dev/null; then echo "PostgreSQL installed successfully!" psql --version else echo "Error: PostgreSQL installation failed" >&2 exit 1 fi echo "============================================"