PostgreSQLライブラリの作成時にCollate,Ctypeを指定する方法

3451 ワード

クラスタの初期化、ライブラリの作成、テーブルの作成、インデックスの作成、sort|compare QUERYの場合、COLLATEを指定できます.使用法の参考https://www.postgresql.org/docs/9.5/static/sql-createtable.html https://www.postgresql.org/docs/9.5/static/collation.html
CREATE DATABASE name
    [ [ WITH ] [ OWNER [=] user_name ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ]
           [ LC_COLLATE [=] lc_collate ]
           [ LC_CTYPE [=] lc_ctype ]
           [ TABLESPACE [=] tablespace_name ]
           [ ALLOW_CONNECTIONS [=] allowconn ]
           [ CONNECTION LIMIT [=] connlimit ] ]
           [ IS_TEMPLATE [=] istemplate ]
CREATE TABLE test1 (
    a text COLLATE "de_DE",
    b text COLLATE "es_ES",
    ...
);
CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ [ IF NOT EXISTS ] name ] ON table_name [ USING method ]
    ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] )
    [ WITH ( storage_parameter = value [, ... ] ) ]
    [ TABLESPACE tablespace_name ]
    [ WHERE predicate ]

Character-string data is sorted according to the collation that applies to the column being sorted. That can be overridden at need by including a COLLATE clause in the expression, for example ORDER BY mycolumn COLLATE "en_US".
クラスタ内で異なるLCまたはテンプレートライブラリと互換性のないLCを作成するにはどうすればいいですか?template 0で作成すると、必要なlcライブラリをすべて新規作成し、テンプレートとして他のライブラリを作成することをお勧めします.
テンプレートライブラリを作成し、collate ps PostgreSQL 9.5以下のバージョンがis_をサポートしないことを指定します.templateオプションは、追加しないでください.
postgres=# create database tmpdb_zh_cn with template template0 lc_collate 'zh_CN.UTF8' lc_ctype 'zh_CN.UTF8' is_template=true;

テンプレートライブラリに、データなど、事前定義が必要なものを作成します.
tmpdb_zh_cn=# create extension hstore;
CREATE EXTENSION

テンプレート・ライブラリに基づいてデータベースを作成します.
postgres=# create database db1_zh_cn with template tmpdb_zh_cn;
CREATE DATABASE

 tmp         | postgres | UTF8     | zh_CN.utf8 | zh_CN.utf8 |                       | 7360 kB | pg_default | 
 tmpdb_zh_cn | postgres | UTF8     | zh_CN.UTF8 | zh_CN.UTF8 |                       | 7360 kB | pg_default | 
(8 rows)

テンプレートライブラリを削除する場合は、テンプレートライブラリを通常ライブラリに変更してから削除する必要があります.
postgres=# alter database tmpdb_zh_cn is_template false;
ALTER DATABASE
postgres=# drop database tmpdb_zh_cn ;
DROP DATABASE

PGでサポートされているcollateのクエリー
postgres=# select * from pg_collation ;
       collname        | collnamespace | collowner | collencoding |      collcollate      |       collctype       
-----------------------+---------------+-----------+--------------+-----------------------+-----------------------
 default               |            11 |        10 |           -1 |                       | 
 C                     |            11 |        10 |           -1 | C                     | C
 POSIX                 |            11 |        10 |           -1 | POSIX                 | POSIX
 aa_DJ                 |            11 |        10 |            6 | aa_DJ.utf8            | aa_DJ.utf8
 aa_DJ                 |            11 |        10 |            8 | aa_DJ                 | aa_DJ
 aa_DJ.iso88591        |            11 |        10 |            8 | aa_DJ.iso88591        | aa_DJ.iso88591
......

クエリ列に対応するcollate pg_catalog.pg_attribute.attcollation対応pg_collation.oid