为什么我的PostgreSQL ORDER BY不区分大小写?

我在Debian上运行Postgres 9.4.4,我得到以下ORDER BY行为:
veure_test=# show LC_COLLATE;
 lc_collate  
-------------
 en_US.UTF-8
(1 row)

veure_test=# SELECT regexp_split_to_table('D d a A c b CD Capacitor',' ') ORDER BY 1;
 regexp_split_to_table 
-----------------------
 a
 A
 b
 c
 Capacitor
 CD
 d
 D
(8 rows)

和uname -a:

Linux ---- 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1 x86_64 GNU/Linux

但是,在我的iMac上,使用Postgres 9.3.4,我得到以下内容

veure_test=# show LC_COLLATE;
 lc_collate  
-------------
 en_US.UTF-8
(1 row)

veure_test=# SELECT regexp_split_to_table('D d a A c b CD Capacitor',' ') ORDER BY 1;
 regexp_split_to_table 
-----------------------
 A
 CD
 Capacitor
 D
 a
 b
 c
 d
(8 rows)

和uname -a:

Darwin ---- 14.4.0 Darwin Kernel Version 14.4.0: Thu May 28 11:35:04 PDT 2015; root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64

我为什么Debian版本似乎不区分大小写而OS X版本不是这样,我感到很困惑.我缺少什么,或者我需要提供哪些其他信息?

更新:在我的Mac上,pg_collat​​ion表显示我有一个en_US.UTF-8排序规则,但在Debian上,我有一个en_US.utf8排序规则.因此,在我的Mac上:

veure_test=# with foo as (
SELECT regexp_split_to_table('D d a A c b CD Capacitor',' ') as bar
   )
SELECT bar FROM foo
ORDER BY bar collate "en_US.UTF-8";                                                                                                                                                                                      
    bar    
-----------
 A
 CD
 Capacitor
 D
 a
 b
 c
 d
(8 rows)

在Debian上:

veure_test=# with foo as (
SELECT regexp_split_to_table('D d a A c b CD Capacitor',' ') as bar
   )
SELECT bar FROM foo
ORDER BY bar collate "en_US.utf8";
    bar    
-----------
 a
 A
 b
 c
 Capacitor
 CD
 d
 D
(8 rows)

那么en_US.UTF-8和en_US.utf8有不同的排序顺序?

So en_US.UTF-8 and en_US.utf8 have different sort orders?

不,这两者都是相同的,只是一个不同的命名约定.

I’m mystified by why the Debian version appears to be case-insensitive and the OS X version is not.

是的,你是对的.这是Mac中的认行为.对于UTF8编码,排序规则不适用于任何BSD-ish操作系统(包括OSX).

以下是证明:

Problems with sort order (UTF8 locales don’t work

正如a_horse_with_no_name所说,Postgres使用操作系统的整理实现.无法在两个操作系统上获得相同的结果.

在你的情况下,你可能(我说可能)这样做:ORDER BY lower(fieldname).

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...