让PG数据库支持中文拼音和笔画排序
Tags: 拼音排序笔画排序PostgreSQLPG
1.前言
默认安装,PG是不支持中文拼音和笔画排序的。
1 2 3 4 5 6 7 8 | postgres=# select * from pg_settings where name ~ 'collate'; name | setting | unit | category | short_desc | extra_desc | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | sourcefile | sourceline | pending_restart ------------+---------+------+----------------+-----------------------------------+------------+----------+---------+----------+-------- -+---------+----------+----------+-----------+------------+------------+----------------- lc_collate | C | | Preset Options | Shows the collation order locale. | | internal | string | override | | | | C | C | | | f (1 row) |
看看示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 | postgres=# create table t(id int, col2 varchar(32)); CREATE TABLE postgres=# insert into t values(1, '东城'), (2, '西城'), (3, '石景山'), (4, '海淀'), (5, '朝阳'); INSERT 0 5 postgres=# select * from t order by col2; id | col2 ----+-------- 1 | 东城 5 | 朝阳 4 | 海淀 3 | 石景山 2 | 西城 (5 rows) |
这是collate=C的排序结果。
2.实现与实践
支持中文排序,需要配置依赖 --with-icu.这就需要提交安装依赖包:libicu-devel libicu
1)、编译安装:
1 2 3 4 5 6 7 8 | wget https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.gz sudo su -c "yum install libicu-devel libicu libxml2-devel libxslt-devel" ./configure --prefix=/usr/pgsql-15-icu --with-icu --with-libxml --with-libxslt --with-openssl make -j 4 world-bin sudo su -c "make install-world-bin" |