天天看點

如何設定資料庫的LC_COLLATE, LC_CTYPE, ENCODING, TEMPLATE

postgresql , create database , collate , ctype , pg_encoding , pg_encoding_to_char()

postgresql執行個體支援建立多個資料庫,建立資料庫時,可以指定模闆庫,并為每個資料庫設定不同的字元集、本地化collate, 貨币等屬性。

接下來舉例講述create database的具體使用方法。

以test資料庫為模闆,克隆一個名為test01的資料庫。

如果不指定模闆,則預設的模闆為template1。

從指定模闆庫建立資料庫,除了目前連接配接,不能有其他使用者連在對應的模闆庫上面。

如果有其他使用者連在test庫,可能會報類似這樣的錯誤

使用者可以參考postgresql的官方文檔,有對應的字元集支援清單

<a href="https://www.postgresql.org/docs/9.6/static/multibyte.html">https://www.postgresql.org/docs/9.6/static/multibyte.html</a>

server=yes表示該字元集支援用于create database。否則隻支援作為用戶端字元集。

name

description

language

server?

bytes/char

aliases

big5

big five

traditional chinese

no

1-2

win950, windows950

euc_cn

extended unix code-cn

simplified chinese

yes

1-3

-

euc_jp

extended unix code-jp

japanese

euc_jis_2004

extended unix code-jp, jis x 0213

euc_kr

extended unix code-kr

korean

euc_tw

extended unix code-tw

traditional chinese, taiwanese

gb18030

national standard

chinese

1-4

gbk

extended national standard

win936, windows936

iso_8859_5

iso 8859-5, ecma 113

latin/cyrillic

1

iso_8859_6

iso 8859-6, ecma 114

latin/arabic

iso_8859_7

iso 8859-7, ecma 118

latin/greek

iso_8859_8

iso 8859-8, ecma 121

latin/hebrew

johab

korean (hangul)

koi8r

koi8-r

cyrillic (russian)

koi8

koi8u

koi8-u

cyrillic (ukrainian)

latin1

iso 8859-1, ecma 94

western european

iso88591

latin2

iso 8859-2, ecma 94

central european

iso88592

latin3

iso 8859-3, ecma 94

south european

iso88593

latin4

iso 8859-4, ecma 94

north european

iso88594

latin5

iso 8859-9, ecma 128

turkish

iso88599

latin6

iso 8859-10, ecma 144

nordic

iso885910

latin7

iso 8859-13

baltic

iso885913

latin8

iso 8859-14

celtic

iso885914

latin9

iso 8859-15

latin1 with euro and accents

iso885915

latin10

iso 8859-16, asro sr 14111

romanian

iso885916

mule_internal

mule internal code

multilingual emacs

sjis

shift jis

mskanji, shiftjis, win932, windows932

shift_jis_2004

shift jis, jis x 0213

sql_ascii

unspecified (see text)

any

uhc

unified hangul code

win949, windows949

utf8

unicode, 8-bit

all

unicode

win866

windows cp866

cyrillic

alt

win874

windows cp874

thai

win1250

windows cp1250

win1251

windows cp1251

win

win1252

windows cp1252

win1253

windows cp1253

greek

win1254

windows cp1254

win1255

windows cp1255

hebrew

win1256

windows cp1256

arabic

win1257

windows cp1257

win1258

windows cp1258

vietnamese

abc, tcvn, tcvn5712, vscii

建立一個utf-8字元集的資料庫

1. 指定的字元集必須是模闆庫字元集的超集,否則會報錯。

2. 指定的lc_ctype和lc_collate必須與目标字元集相容。

例子,template1是預設模闆庫,它的字元集為utf8。

建立一個euc_cn字元集的資料庫

報錯1,euc_cn字元集與模闆庫的lc_collate,lc_ctype不相容。

報錯2,euc_cn字元集與模闆庫的字元集utf-8不相容。

使用如下sql可以查詢系統表pg_collation得到字元集支援的lc_collate和lc_ctype。

其中encoding為空時,表示這個collation支援所有的字元集。

建立一個資料庫,lc_collate, lc_ctype分别為zh_cn.utf8

如果指定的lc_collate, lc_ctype與模闆庫的collate,ctype不相容,會報錯。

解決辦法1,使用相容的collate和ctype。

解決辦法2,使用template0作為模闆庫。

目前無法直接通過alter database的文法進行修改,使用者可以使用建立新的資料庫,導出,再導入的方式。

1. 建立新資料庫,指定目标collate和ctype

2. 使用pg_dump或其他用戶端工具邏輯導出源資料庫的資料

3. 使用pg_restore或其他用戶端工具,将第二步導出資料導入新資料庫

<a href="https://www.postgresql.org/docs/9.6/static/sql-createdatabase.html">https://www.postgresql.org/docs/9.6/static/sql-createdatabase.html</a>