#!/usr/bin/env bash # Fix PostgreSQL "collation version mismatch" (e.g. DB created with 2.36, OS provides 2.41). # Reindexes then refreshes collation version. REINDEX will be canceled if Gitea (or other # clients) keep using the DB, so either stop Gitea first or use --terminate-connections. # # Usage (from host): # ./fix-postgres-collation-version.sh # ./fix-postgres-collation-version.sh gitea # ./fix-postgres-collation-version.sh gitea --terminate-connections # # Option: --terminate-connections Terminate all other sessions on the DB so REINDEX can # run without being canceled. Gitea will disconnect; restart it after. # REINDEX DATABASE gitea; # ALTER DATABASE gitea REFRESH COLLATION VERSION; set -e CONTAINER="${1:?Usage: $0 [database_name] [--terminate-connections]}" DB="${2:-gitea}" TERMINATE="" if [[ "$2" == "--terminate-connections" ]]; then TERMINATE=1 DB="gitea" elif [[ "$3" == "--terminate-connections" ]]; then TERMINATE=1 fi if [[ -z "$TERMINATE" ]]; then echo "Stop the Gitea container (and any other app using database $DB) before continuing," echo "otherwise REINDEX may be canceled. Press Enter when ready, or Ctrl+C to abort." read -r fi if [[ -n "$TERMINATE" ]]; then echo "Terminating other connections to $DB..." docker exec "$CONTAINER" psql -U postgres -d "$DB" -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '$DB' AND pid <> pg_backend_pid();" || true sleep 2 fi echo "Reindexing database $DB (may take a while)..." docker exec "$CONTAINER" psql -U postgres -d "$DB" -c "REINDEX DATABASE $DB;" echo "Refreshing collation version..." docker exec "$CONTAINER" psql -U postgres -d "$DB" -c "ALTER DATABASE $DB REFRESH COLLATION VERSION;" echo "Done. Start Gitea again if you stopped it or used --terminate-connections."