Fix PostgreSQL Collation Version Mismatch in Kali Linux

The warning that a database has a collation version mismatch means the operating system's collation data (from glibc on Kali) changed after the database was created, usually after a rolling upgrade. Text sort order may now differ, so indexes on text columns could be inconsistent.

Mar 28, 2025 - 14:34
Updated: 
111.6k

Security Tools, Authorized Labs & CTFs in Cybersecurity & Ethical Hacking · Troubleshooting

Data Analytics & Data Science, Linux Administration

Fix PostgreSQL Collation Version Mismatch in Kali Linux

Quick answer: The warning that a database has a collation version mismatch means the operating system's collation data (from glibc on Kali) changed after the database was created, usually after a rolling upgrade. Text sort order may now differ, so indexes on text columns could be inconsistent. The safe fix is to back up, rebuild affected indexes with REINDEX DATABASE, then record the new version with ALTER DATABASE name REFRESH COLLATION VERSION. Repeat for each database that warns, including postgres and template1. The refresh command alone only silences the warning; the PostgreSQL documentation says objects depending on the collation should be rebuilt first.

Key takeaways

  • The warning comes from a library upgrade, not from Metasploit.
  • Back up before any change.
  • Reindex first, refresh the recorded version second.
  • ALTER DATABASE ... REFRESH COLLATION VERSION needs PostgreSQL 15 or later.
  • Fix every database that shows the warning, including templates.

Table of Contents

Why Does the Collation Warning Appear on Kali?

PostgreSQL uses the operating system's locale data to sort and compare text. When a database is created it records the collation version. Kali is a rolling release, so a routine upgrade can ship a new glibc with changed sort rules. On the next connection PostgreSQL compares the stored version with the current one and warns if they differ, as described in the collation support documentation.

Is the Warning Dangerous or Just Noise?

It is a real signal. A B-tree index on text is ordered using the old rules. If the rules changed, lookups may miss rows or unique constraints may not catch duplicates. The ALTER COLLATION page explains that refreshing the version does not check or fix those objects. For a small lab database the risk is low, but the correct order still matters.

How Do You Find Which Databases Are Affected?

pg_lsclusters
sudo -u postgres psql -c 'SELECT datname, datcollversion, pg_database_collation_actual_version(oid) FROM pg_database;'

Rows where the two version columns differ need attention. pg_lsclusters shows the cluster version and status on Debian-based systems such as Kali.

How Do You Fix the Mismatch Step by Step?

  1. Back up with pg_dump.
  2. Rebuild indexes in the affected database using REINDEX.
  3. Refresh the recorded version with ALTER DATABASE.
  4. Repeat for postgres and template1, then reconnect to confirm the warning is gone.
sudo -u postgres pg_dump -Fc msf -f /tmp/msf-backup.dump
sudo -u postgres psql -d msf -c 'REINDEX DATABASE msf;'
sudo -u postgres psql -d msf -c 'ALTER DATABASE msf REFRESH COLLATION VERSION;'
sudo -u postgres psql -d postgres -c 'ALTER DATABASE postgres REFRESH COLLATION VERSION;'
sudo -u postgres psql -d template1 -c 'ALTER DATABASE template1 REFRESH COLLATION VERSION;'

REINDEX DATABASE must be run while connected to that database. It takes locks, so run it when nothing else uses the database. The postgres and template1 databases usually hold no user indexes on text, which is why many guides only refresh them.

What Errors Can You Hit While Fixing It?

MessageCauseFix
Syntax error at REFRESHPostgreSQL older than 15Upgrade the cluster or use ALTER COLLATION per collation
Must be owner of databaseNot running as postgresPrefix with sudo -u postgres
Can only reindex the currently open databaseConnected to a different databaseAdd -d with the database name
Warning returns on a new databasetemplate1 not refreshedRefresh template1 too

How Does This Affect Metasploit?

Metasploit only shows the warning because it connects to PostgreSQL. Once the msf database is fixed, msfconsole connects without it. If the database will not connect at all after the upgrade, check the service first, then see our Metasploit and PostgreSQL setup guide and the note on the connection already established message. The Kali documentation covers msfdb start-up.

How Can You Reduce Repeat Warnings?

PracticeBenefit
Check pg_database after each full upgradeCatches mismatches early
Snapshot the lab VM before upgradingEasy rollback
Keep regular pg_dump backupsData survives a broken cluster
Consider ICU collations for new databasesVersioned separately from glibc

To build confident Linux and database skills for security labs, ask about WebAsha Kali Linux and ethical hacking training.

References

Frequently Asked Questions

The operating system’s collation data, usually from glibc, changed after the database was created, often during a system upgrade.

It removes the warning but does not repair indexes. PostgreSQL documentation advises rebuilding dependent objects such as indexes first.

PostgreSQL 15 and later. Older versions return a syntax error, though individual collations can be refreshed with ALTER COLLATION.

Yes, if they warn. Refreshing template1 also stops new databases created from it from inheriting the old version.

Yes, standard REINDEX takes locks that block writes to the affected tables, so run it when the database is not in use.

No. The data is still there. The warning concerns sort order and indexes, which REINDEX rebuilds from the stored data.

Query pg_database and compare datcollversion with pg_database_collation_actual_version for each database in the cluster.

Yes. A pg_dump backup or a virtual machine snapshot lets you recover quickly if anything goes wrong during maintenance.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.