PostgreSQL: How to solve collation mismatch

This is how to solve a collation mismatch in your PostgreSQL database

PostgreSQL: How to solve collation mismatch

When you're keeping your PostgreSQL database up to date with the latest binaries, you sometimes get this error:

WARNING: database "postgres" has a collation version mismatch
DETAIL: The database was created using collation version 2.40, but the operating system provides version 2.41.
HINT: Rebuild all objects in this database that use the default collation and run ALTER DATABASE postgres REFRESH COLLATION VERSION, or build PostgreSQL with the right library version.

Or if you installed your system using another language than English, it looks like that (mine is in German):

WARNUNG:  Version von Sortierfolge für Datenbank »postgres« stimmt nicht überein
DETAIL:  Die Datenbank wurde mit Sortierfolgenversion 2.40 erzeugt, aber das Betriebssystem hat Version 2.41.
TIP:  Bauen Sie alle Objekte in dieser Datenbank, die die Standardsortierfolge verwenden, neu und führen Sie ALTER DATABASE postgres REFRESH COLLATION VERSION aus, oder bauen Sie PostgreSQL mit der richtigen Bibliotheksversion.

As I'm a fan of using the binaries provided by PostgreSQL, I won't build PostgreSQL by myself. So I'll try a REFRESH:

postgres=# ALTER DATABASE postgres REFRESH COLLATION VERSION;

That takes less than a second - maybe because the database postgres is quite small:

NOTICE:  changing version from 2.40 to 2.41
ALTER DATABASE

Or here, my German version:

HINWEIS:  Version wird von 2.40 in 2.41 geändert
ALTER DATABASE

Some also recommend to REINDEX the database or the system:

postgres=# reindex database postgres;
REINDEX
postgres=# reindex system;
REINDEX