RDS for PostgreSQL 排序规则

数据库关系型数据库技术服务知识库
前言

在 PostgreSQL 中可以设置字符串排序,字符归类方法,数值格式,日期格式,时间格式,货币格式。比较常见的是字符串排序以及字符分类

LC_COLLATEString sort order
LC_CTYPE字符分类
LC_MESSAGES消息的语言
LC_MONETARY货币使用的格式
LC_NUMERIC数字使用的格式
LC_TIME时间日期使用的格式
查看字符集支持的LC_COLLATE和LC_CTYPE
dbtest=# select pg_encoding_to_char(collencoding) as encoding,collname,collcollate,collctype from pg_collation;
 encoding |        collname        |   collcollate    |    collctype     
----------+------------------------+------------------+------------------
          | default                |                  | 
          | C                      | C                | C
          | POSIX                  | POSIX            | POSIX
 UTF8     | ucs_basic              | C                | C
 UTF8     | C.utf8                 | C.utf8           | C.utf8
 UTF8     | en_AG                  | en_AG            | en_AG
 LATIN1   | en_AU                  | en_AU            | en_AU
 UTF8     | en_AU.utf8             | en_AU.utf8       | en_AU.utf8
 LATIN1   | en_BW                  | en_BW            | en_BW
 UTF8     | en_BW.utf8             | en_BW.utf8       | en_BW.utf8
 LATIN1   | en_CA                  | en_CA            | en_CA
 UTF8     | en_CA.utf8             | en_CA.utf8       | en_CA.utf8
 LATIN1   | en_DK                  | en_DK            | en_DK
 UTF8     | en_DK.utf8             | en_DK.utf8       | en_DK.utf8
 LATIN1   | en_GB                  | en_GB            | en_GB
 LATIN9   | en_GB.iso885915        | en_GB.iso885915  | en_GB.iso885915
 UTF8     | en_GB.utf8             | en_GB.utf8       | en_GB.utf8
...

注: 我们看到encoding 列有的为空值,那么代表这个 collation 支持所有的字符集。

查看当前数据库所使用的字符集
dbtest=# select datname,pg_encoding_to_char(encoding) as encoding from pg_database;
  datname  | encoding 
-----------+----------
 postgres  | UTF8
 template1 | UTF8
 template0 | UTF8
 dbtest    | UTF8
 dbtest1   | UTF8
(5 rows)

或者您可以使用\l 命令来查看

dbtest=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf-8 | en_US.utf-8 | =Tc/postgres         +
           |          |          |             |             | postgres=CTc/postgres
 dbtest    | dbtest   | UTF8     | en_US.utf-8 | en_US.utf-8 | 
 dbtest1   | dbtest   | UTF8     | en_US.utf-8 | en_US.utf-8 | 
 template0 | postgres | UTF8     | en_US.utf-8 | en_US.utf-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf-8 | en_US.utf-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(5 rows)
如何在创建表时指定列的collate
dbtest=# create table t1(id int, name varchar(10) collate "en_IL");
CREATE TABLE

dbtest=# \d t1;
                        Table "public.t1"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 id     | integer               |           |          | 
 name   | character varying(10) | en_IL     |          |
如何修改列的collate
alter table t1 alter col1 type text COLLATE "en_US";

注: 修改列的collate 会进行rewrite table,建议您在业务低峰期执行此操作。 如果您有其他问题,欢迎您联系火山引擎技术支持服务

25
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论