Skip to content

Instantly share code, notes, and snippets.

@santattech
Last active June 2, 2023 08:00
Show Gist options
  • Save santattech/bd16fdd75ad5016bac53c34020a0a635 to your computer and use it in GitHub Desktop.
Save santattech/bd16fdd75ad5016bac53c34020a0a635 to your computer and use it in GitHub Desktop.
Postgres collation version issue

Postgres collation version issue

What is PG collation?

A collation is an SQL schema object that maps an SQL name to operating system locales. A collation specifies the bit patterns that represent each character in a dataset and determine the rules used to sort and compare data. Collation attributes can have an impact on query results because they govern how sorting and comparison operations function.

How do I get collation in PostgreSQL?

To find the collation of the database, you need to query pg_database : select datname, datcollate from pg_database;

What was the error?

Recently I have updated my OS version from Ubunutu 20 to Ubuntu 22. After that whenever I tried to create a database in my Postgresql, it throws error. Below is the error shown:

The template database was created using collation version 2.31, 
but the operating system provides version 2.35.

How we solved?

To solve that you have find all the databases in your localhost. List them and update their collation version one by one, by rebuilding all objects in the template database that use the default collation. Then you need to restart the POSTGRESQL service once. I am talking about the systemd service.

  1. Open postgres console
sudo -u postgres psql
  1. List down the databases: \l
  2. Execute for all the databases one by one. My DB name is sc_test
ALTER DATABASE sc_test REFRESH COLLATION VERSION;
  1. At last do it for postgres DB

ALTER DATABASE postgres REFRESH COLLATION VERSION;

  1. Restart the service: sudo service postgresql restart
  2. Now you can create your DB again. create database fmdp_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment