postgresql 如何更改模板数据库集合编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18870775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:25:09  来源:igfitidea点击:

How to change the template database collection coding

postgresqlpostgresql-9.2

提问by user504909

I want build new postgreSQL database by:

我想通过以下方式构建新的 postgreSQL 数据库:

CREATE DATABASE newdb
WITH OWNER = postgres
   ENCODING = 'UTF8'
   TABLESPACE = pg_default
   LC_COLLATE = 'zh_CN.UTF-8'
   CONNECTION LIMIT = -1;

and the error is:

错误是:

ERROR: new collation (zh_CN.UTF-8) is incompatible with the collation of the template database (en_US.UTF8)
HINT: Use the same collation as in the template database, or use template0 as template.

错误:新排序规则 (zh_CN.UTF-8) 与模板数据库 (en_US.UTF8) 的排序规则不兼容
提示:使用与模板数据库中相同的排序规则,或使用 template0 作为模板。

How to change the template database collection?

如何更改模板数据库集合?

回答by Tomas Greif

From PostgreSQLdocumentation:

来自PostgreSQL文档:

Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.

复制 template0 而不是 template1 的另一个常见原因是在复制 template0 时可以指定新的编码和区域设置,而 template1 的副本必须使用与它相同的设置。这是因为 template1 可能包含特定于编码或特定于语言环境的数据,而 template0 已知不包含。

You can use only template0to create new database with different encoding and locale:

您只能template0用于创建具有不同编码和语言环境的新数据库:

CREATE DATABASE newdb
WITH OWNER = postgres
   ENCODING = 'UTF8'
   TABLESPACE = pg_default
   LC_COLLATE = 'zh_CN.UTF-8'
   CONNECTION LIMIT = -1
   TEMPLATE template0;

This will work, however it means that any changes you made to template1won't be applied to newly created database.

这会起作用,但这意味着您所做的任何更改template1都不会应用于新创建的数据库。

To change encoding and collation of template1you have to first delete template1and then create new template template1from template0. How to drop template database is described here. Then you can create new database template1with chosen encoding/collation and mark it as a template by setting datistemplate=true(example):

要更改编码和排序规则,template1您必须先删除template1,然后template1template0. 此处描述如何删除模板数据库。然后,您可以template1使用选定的编码/排序规则创建新数据库,并通过设置datistemplate=true示例)将其标记为模板:

update pg_database set datistemplate=true  where datname='template1';

回答by david.perez

Tomas answer is correct, but it is missing an important detail (LC_CTYPE). Here is the full sequence for recreating template1with the correct locale:

Tomas 的回答是正确的,但它遗漏了一个重要的细节 ( LC_CTYPE)。以下是template1使用正确语言环境重新创建的完整序列:

ALTER database template1 is_template=false;

DROP database template1;

CREATE DATABASE template1
WITH OWNER = postgres
   ENCODING = 'UTF8'
   TABLESPACE = pg_default
   LC_COLLATE = 'zh_CN.UTF-8'
   LC_CTYPE = 'zh_CN.UTF-8'
   CONNECTION LIMIT = -1
   TEMPLATE template0;

ALTER database template1 is_template=true;

then you can create new databases.

然后你可以创建新的数据库。