Postgres Order By: the resultset is sorted differently

dm8ry

Dmitry Romanoff

Posted on November 1, 2023

Postgres Order By: the resultset is sorted differently

When querying different databases of Postgres the resultset is ordered differently. It happens because of differences in the datcollate and datctype.

For example:

Postgres DB Server #1

select datcollate, datctype from pg_database where datname='<my_db>';

         datcollate         |          datctype          
----------------------------+----------------------------
 English_United States.1252 | English_United States.1252
Enter fullscreen mode Exit fullscreen mode

Postgres DB Server #2

select datcollate, datctype from pg_database where datname='<my_db>';

 datcollate |  datctype  
------------+------------
 en_US.utf8 | en_US.utf8
Enter fullscreen mode Exit fullscreen mode

In case names contain both capital and small letters, the resultset will be ordered differently. For example, when querying the following:

SELECT table_name, column_name, data_type, is_nullable, character_maximum_length, column_default
FROM information_schema.columns
WHERE table_schema = 'public' and table_name != 'pg_stat_statements'
ORDER BY table_name, ordinal_position;
Enter fullscreen mode Exit fullscreen mode

Postgres DB Server #1

...
dmitrYtest
dmitry
...
Enter fullscreen mode Exit fullscreen mode

Postgres DB Server #2

...
dmitry
dmitrYtest
...
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
dm8ry
Dmitry Romanoff

Posted on November 1, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related