ロケールの設定を行うと、文字列処理、日付や通貨の書式、メッセージの言語などを 変更できる。
ロケールをサポートしない C ロケール(--locale=C と --no-locale は同じ)以外に 設定すると不具合が生じる場合がある。
文字列の並び換え順(lc_collate)と文字の分類(lc_ctype)は、データベース・ クラスタ初期化(initdb)時とデータベース作成時のみ設定出来る。後からの変更 は出来ない。
C ロケール(--locale=C または --no-locale)以外にする場合、エンコーディング とロケールは一致しなければならない。
・データベース・クラスタ初期化(initdb)時設定項目は、全てのロケール
    [postgres]$ initdb --encoding=UTF-8 --no-locale --data-checksums
    [postgres]$ initdb --encoding=UTF-8 --locale=ja_JP.UTF-8 --data-checksums
    確認
    DB=> select name, setting, context from pg_settings where name like 'lc%';
| name (ロケール名) | setting (設定) | context (設定変更) | 
|---|---|---|
| lc_collate | C | internal(変更出来ない)文字列の並び換え順 | 
| lc_ctype | C | internal(変更出来ない)文字の分類 | 
| lc_messages | C | superuser(管理者)メッセージの言語 | 
| lc_monetary | C | user(一般ユーザー)通貨書式 | 
| lc_numeric | C | user(一般ユーザー)数値書式 | 
| lc_time | C | user(一般ユーザー)日付/時刻の書式 | 
設定項目は、全てのロケール
    [user]$ createdb -U ユーザー名 データベース名
                    --encoding=UTF-8 --locale=ja_JP.UTF-8 --template=template0
    確認
    DB=> \l
| データベース一覧 | ||||
|---|---|---|---|---|
| 名前 | 所有者 | エンコーディング | 照合順序 | Ctype (変換演算子) | 
| postgres | postgres | UTF8 | C | C | 
| template0 | postgres | UTF8 | C | C | 
| dbname | username | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | 
設定項目は、lc_messages, lc_monetary, lc_numeric, lc_time
    [root]# vim /usr/local/pgsql/data/postgresql.conf
      lc_messages = 'C'   # メッセージの言語
      lc_monetary = 'C'   # 通貨書式
      lc_numeric = 'C'   # 数値書式
      #lc_time = 'C'   # 日付/時刻の書式
      lc_time = 'ja_JP.UTF-8'
    [root]# systemctl reload-or-restart postgresql.service
    確認
    DB=> select name, setting, context from pg_settings where name like 'lc%';
| name | setting | context | 
|---|---|---|
| lc_collate | C | internal | 
| lc_ctype | C | internal | 
| lc_messages | C | superuser | 
| lc_monetary | C | user | 
| lc_numeric | C | user | 
| lc_time | ja_JP.UTF-8 | user | 
    DB=> set  LC_TIME = 'ja_JP.UTF-8';   # 日付/時刻の書式
    確認
    DB=> show lc_time;
       lc_time
    ------------------
     ja_JP.UTF-8
    DB=> select to_char( current_timestamp, 'MM/DD(TMDy)' ) as today;
       today
    ----------------
     08/06(土)
    Perl
    my $sql = "set LC_TIME = 'ja_JP.UTF-8';";   # 日付/時刻の書式
    $sql   .= " select to_char( 列名, 'MM/DD(TMDy)' ) as 別名 from テーブル";