#!/usr/bin/env bash set -euo pipefail # ========================== # CONFIG # ========================== PG_VERSION="${PG_VERSION:-16}" LOCALE="en_US.UTF-8" TIMEZONE="${TIMEZONE:-UTC}" # ========================== # CHECK ROOT # ========================== if [[ "$EUID" -ne 0 ]]; then echo "❌ Please run as root" exit 1 fi echo "✅ Running as root" # ========================== # CHECK wget # ========================== if ! command -v wget >/dev/null 2>&1; then echo "❌ wget not found" exit 1 fi # ========================== # SET LOCALE UTF-8 # ========================== echo "🌍 Setting system locale to ${LOCALE}" apt update apt install -y locales sed -i "s/^# *${LOCALE}/${LOCALE}/" /etc/locale.gen locale-gen "${LOCALE}" update-locale LANG="${LOCALE}" export LANG="${LOCALE}" export LC_ALL="${LOCALE}" # ========================== # SET TIMEZONE (OPTIONAL BUT GOOD) # ========================== echo "⏰ Setting timezone to ${TIMEZONE}" timedatectl set-timezone "${TIMEZONE}" || true # ========================== # ADD POSTGRES APT REPO (OFFICIAL) # ========================== echo "📦 Adding PostgreSQL official repository" apt install -y gnupg lsb-release ca-certificates wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc \ | gpg --dearmor -o /usr/share/keyrings/postgresql.gpg echo "deb [signed-by=/usr/share/keyrings/postgresql.gpg] \ http://apt.postgresql.org/pub/repos/apt \ $(lsb_release -cs)-pgdg main" \ > /etc/apt/sources.list.d/postgresql-pgdg.list apt update # ========================== # INSTALL POSTGRESQL # ========================== echo "🐘 Installing PostgreSQL ${PG_VERSION}" apt install -y \ postgresql-${PG_VERSION} \ postgresql-client-${PG_VERSION} \ postgresql-contrib-${PG_VERSION} \ postgresql-server-dev-${PG_VERSION} \ postgresql-${PG_VERSION}-postgis-3 postgis \ libpq-dev # ========================== # VERIFY ENCODING # ========================== echo "🔍 Verifying PostgreSQL encoding" sudo -u postgres psql -tAc "SHOW server_encoding;" | grep -qi utf8 \ && echo "✅ PostgreSQL server encoding is UTF8" \ || echo "⚠️ WARNING: PostgreSQL encoding is not UTF8" # ========================== # SHOW CLUSTER INFO # ========================== pg_lsclusters echo "🎉 PostgreSQL ${PG_VERSION} installed successfully with UTF-8 locale"