View and modify character sets postgresql

A view character set

automatic character set conversion rule between character sets supported pg 10, and service segments and client reference  http://www.postgres.cn/docs/10/multibyte.html

1. Examples of character sets server

postgres=# show server_encoding;
 server_encoding 
-----------------
 UTF8

2. The database character set

postgres=# \l
-- 或者
postgres=# select datname,pg_encoding_to_char(encoding) as encoding from pg_database;

3. The client character set

postgres=# show client_encoding;
 client_encoding 
-----------------
 UTF8

-- 或者

postgres=# \encoding
UTF8

 

Second, set the character set

1. Examples of character sets server

Only when the initialize an instance may be provided

initdb -D /data_dir -E UTF8 --locale=en_US.utf8
  • -E encoding --encoding = encoding or
    encoding choose a template database, which will be the default encoding at database created in the future, unless it appears to cover when creating a database. This value is obtained from the default locale (--locale) in, if not locale, default SQL_ASCII.
  • --locale = locale
    setting area server instance, the default operating system from the environment of operation initdb

After initialization error will be modified

postgres=# alter system set server_encoding = 'UTF8';
ERROR:  parameter "server_encoding" cannot be changed

If at the instance initialization set the wrong character set and can not re-initialized, a workaround is to drop template1 database, specify the encoding reconstruction

UPDATE pg_database SET datistemplate=FALSE WHERE datname='template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH owner=postgres template=template0 encoding='UTF8';
UPDATE pg_database SET datistemplate=TRUE WHERE datname='template1';

 

2. The database character set

  • Specified when created using the createdb command
createdb -E EUC_KR -T template0 --lc-collate=ko_KR.euckr --lc-ctype=ko_KR.euckr korean
  • Specified when creating db using sql

Note the following command to copy specified template0database. When a copy of any other database can not be changed from the source database come encoding and locale, as this may cause damage to data.

CREATE DATABASE korean WITH ENCODING 'EUC_KR' LC_COLLATE='ko_KR.euckr' LC_CTYPE='ko_KR.euckr' TEMPLATE=template0;

 

3. The client character set

  • Operating system layer settings
export PGCLIENTENCODING=GBK
  • Or set db
set client_encoding to 'utf8';
-- 或者
\encoding GBK

 

reference

https://blog.csdn.net/daijiguo/article/details/82767789

http://www.postgres.cn/docs/10/multibyte.html

https://blog.51cto.com/wujianwei/1979023

Published 295 original articles · won praise 35 · views 80000 +

Guess you like

Origin blog.csdn.net/Hehuyi_In/article/details/105207784