setup_env/install_postgres.sh
Paul Trowbridge abaf266904 improve install scripts with modern practices and transparency
- Use modern signed-by method for PostgreSQL GPG keys
- Auto-detect latest Python 3 version from deadsnakes PPA
- Add command tracing (set -x) to show all commands as they run
- Display sudo commands upfront before execution
- Add user confirmation prompts (y/N) before installation
- Improve error handling with set -euo pipefail
- Add proper cleanup and verification steps
- Use pipx for VisiData installation
- Better output formatting with clear section headers

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-17 23:28:44 -05:00

62 lines
1.7 KiB
Bash
Executable File

#!/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 "============================================"