#!/usr/bin/env python3 """Securely update the PostgreSQL password for the headquarter database user. Usage: python scripts/update_db_password.py The script will prompt for the new password (no echo) and update it via the running hq-postgres Docker container. After running, remember to update your .env file: POSTGRES_PASSWORD= """ import getpass import subprocess import sys def main() -> None: # Prompt for new password securely (no echo to terminal) new_password = getpass.getpass("Enter new password for 'headquarter' user: ") if not new_password: print("Error: password cannot be empty.", file=sys.stderr) sys.exit(1) confirm = getpass.getpass("Confirm new password: ") if new_password != confirm: print("Error: passwords do not match.", file=sys.stderr) sys.exit(1) # Use psql inside the running postgres container to avoid exposing # the password in host shell history. sql = f"ALTER ROLE headquarter WITH PASSWORD '{new_password}';" try: result = subprocess.run( [ "docker", "exec", "-i", "hq-postgres", "psql", "-U", "headquarter", "-d", "headquarter", "-c", sql, ], capture_output=True, text=True, check=True, ) print(result.stdout.strip()) print("\n✅ Password updated successfully.") print("\n⚠️ IMPORTANT: Update your .env file:") print(f" POSTGRES_PASSWORD={new_password}") print("\n⚠️ Then restart the application containers:") print(" docker compose up -d") except subprocess.CalledProcessError as exc: print(f"Error: {exc.stderr or exc.stdout}", file=sys.stderr) sys.exit(1) except FileNotFoundError: print( "Error: 'docker' command not found. Is Docker installed and running?", file=sys.stderr, ) sys.exit(1) if __name__ == "__main__": main()