简体   繁体   English

如何在 Windows 上的 PostgreSQL 中使用 UTF-8 排序规则创建数据库?

[英]How to create a database with UTF-8 collation in PostgreSQL on Windows?

I'm configuring PostgreSQL db for the Bitbucket Server on Windows.我正在为 Windows 上的 Bitbucket 服务器配置 PostgreSQL 数据库。 In the official guide it says that:在官方指南中它说:

The database must be configured to use the UTF-8 character set.必须将数据库配置为使用 UTF-8 字符集。

It doesn't strictly say that you have to set collation to UTF-8, but for other atlassian procucts it's recommended so I assume that's the same case for Bitbucket Server.它并没有严格说您必须将整理设置为 UTF-8,但是对于其他 Atlassian 产品,建议使用它,因此我认为 Bitbucket Server 的情况也是如此。 Exmaple from Confluence documentation: Confluence 文档中的示例:

  • Character encoding must be set to utf8 encoding.字符编码必须设置为utf8编码。
  • Collation must also be set to utf8.排序规则也必须设置为 utf8。 Other collations, such as "C", are known to cause issues with Confluence.已知其他排序规则(例如“C”)会导致 Confluence 出现问题。

This is what I have now, the problem is that it sets the collation to English_United States.1252 :这就是我现在所拥有的,问题是它将排序规则设置为English_United States.1252

CREATE DATABASE test
WITH OWNER "postgres"
ENCODING 'UTF8'
LC_COLLATE = 'american_usa'
LC_CTYPE = 'american_usa'
TEMPLATE template0;

Is setting collation to UTF-8 actually necessary and if yes, how can I do it?是否真的需要将排序规则设置为 UTF-8,如果是,我该怎么做?

Assuming that you are trying to create a PosgreSQL database with US locale sort order and character classification with UTF-8 encoding on Windows, following is a modification to the code example posted in the original question that may be used to achieve that result.假设您尝试在 Windows 上使用美国语言环境排序顺序和字符分类使用 UTF-8 编码创建 PosgreSQL 数据库,以下是对原始问题中发布的代码示例的修改,可用于实现该结果。

CREATE DATABASE "example_db"
WITH OWNER "postgres"
ENCODING 'UTF8'
LC_COLLATE = 'en-US'
LC_CTYPE = 'en-US'
TEMPLATE template0;

One liner format for terminal copy / paste:终端复制/粘贴的一种衬垫格式:

CREATE DATABASE "example_db" WITH OWNER "postgres" ENCODING 'UTF8' LC_COLLATE = 'en-US' LC_CTYPE = 'en-US' TEMPLATE template0;

For anyone trying to create a similar database in a Linux environment such as Ubuntu on Windows Subsystem for Linux, you can do the following (depending on the specific environment, you may need to use 'en_US.UTF8' as the locale instead):对于任何试图在 Linux 环境(例如 Windows Subsystem for Linux 上的 Ubuntu)中创建类似数据库的人,您可以执行以下操作(根据特定环境,您可能需要使用'en_US.UTF8'作为语言环境):

CREATE DATABASE "example_db"
WITH OWNER "postgres"
ENCODING 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8'
TEMPLATE template0;

One liner format for terminal copy / paste:终端复制/粘贴的一种衬垫格式:

CREATE DATABASE "example_db" WITH OWNER "postgres" ENCODING 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8' TEMPLATE template0;

There is no UTF8 collation.没有 UTF8 整理。 UTF8 is a way to encode characters as numbers, a so-called encoding . UTF8 是一种将字符编码为数字的方法,即所谓的编码 Collations define how characters (and composites) are ordered.排序规则定义了字符(和组合)的排序方式。

While you have to pick a collation that matches the database encoding with PostgreSQL on UNIX, that is not required on Windows.虽然您必须选择与 UNIX 上的 PostgreSQL 数据库编码匹配的排序规则,但在 Windows 上则不需要。 Maybe the documentation you are reading is targeted at UNIX.也许您正在阅读的文档是针对 UNIX 的。

You should ask the people who wrote the software to tell you what collation to use.您应该询问编写软件的人告诉您要使用的归类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM