{"Entry":{"collection":"sql","key":"create-database","name":"CREATE DATABASE","aliases":["createdatabase"],"metadata":{"aliases":["createdatabase"],"changed_in":["7.1","7.3","8.0","8.1","8.4","9.0","9.2","9.5","10","13","15","16","17"],"changes":[{"from":"6.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","usage","compatibility"],"removed":["bugs"]},"status":"changed","synopsis":null,"to":"7.0"},{"from":"7.0","purpose_changed":false,"renamed":{"from_file":"sql-createdatabase.htm","to_file":"sql-createdatabase.html"},"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":{"added":["[ WITH [ LOCATION = 'dbpath' ]","[ TEMPLATE = template ]","[ ENCODING = encoding ] ]"],"removed":[]},"to":"7.1"},{"from":"7.1","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":["description","usage"],"removed":[]},"status":"changed","synopsis":null,"to":"7.2"},{"from":"7.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ [ WITH ] [ OWNER [=] dbowner ]","[ LOCATION [=] 'dbpath' ]","[ TEMPLATE [=] template ]","[ ENCODING [=] encoding ] ]"],"removed":[]},"to":"7.3"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":["parameters","notes","examples"],"changed":["description","compatibility"],"removed":["usage"]},"status":"changed","synopsis":null,"to":"7.4"},{"from":"7.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["[ TABLESPACE [=] tablespace ] ]"],"removed":["[ LOCATION [=] 'dbpath' ]","[ TEMPLATE [=] template ]"]},"to":"8.0"},{"from":"8.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["[ CONNECTION LIMIT [=] connlimit ] ]"],"removed":[]},"to":"8.1"},{"from":"8.1","purpose_changed":false,"renamed":null,"sections":{"added":["see_also"],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"8.2"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"8.3"},{"from":"8.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["[ LC_COLLATE [=] lc_collate ]","[ LC_CTYPE [=] lc_ctype ]","[ TABLESPACE [=] tablespace ]"],"removed":[]},"to":"8.4"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["[ [ WITH ] [ OWNER [=] user_name ]"],"removed":["[ [ WITH ] [ OWNER [=] dbowner ]"]},"to":"9.0"},{"from":"9.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"9.1"},{"from":"9.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["[ TABLESPACE [=] tablespace_name ]"],"removed":["[ TABLESPACE [=] tablespace ]"]},"to":"9.2"},{"from":"9.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.4"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["[ ALLOW_CONNECTIONS [=] allowconn ]","[ CONNECTION LIMIT [=] connlimit ]","[ IS_TEMPLATE [=] istemplate ] ]"],"removed":[]},"to":"9.5"},{"from":"9.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.6"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":[],"removed":["[ [ WITH ] [ OWNER [=] user_name ]","[ IS_TEMPLATE [=] istemplate ] ]"]},"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"11"},{"from":"12","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","examples"],"removed":[]},"status":"changed","synopsis":{"added":["[ ENCODING [=] encoding ]","[ LOCALE [=] locale ]"],"removed":[]},"to":"13"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"14"},{"from":"14","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["[ STRATEGY [=] strategy ]","[ LOCALE [=] locale ]","[ LC_CTYPE [=] lc_ctype ]","[ ICU_LOCALE [=] icu_locale ]","[ LOCALE_PROVIDER [=] locale_provider ]","[ COLLATION_VERSION = collation_version ]","[ OID [=] oid ]"],"removed":[]},"to":"15"},{"from":"15","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["[ ICU_RULES [=] icu_rules ]","[ LOCALE_PROVIDER [=] locale_provider ]"],"removed":[]},"to":"16"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["[ LC_CTYPE [=] lc_ctype ]","[ BUILTIN_LOCALE [=] builtin_locale ]"],"removed":[]},"to":"17"},{"from":"17","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"18"}],"content_hash":"66f09aa6b01c6ad6374f141b26a705d8d233fd2543c3bb25b3c9b3db1eb47883","editorial":{},"first_version":"6.4","group":"schema","imported_at":"2026-09-30T17:43:37.740673+08:00","last_version":"20","name":"CREATE DATABASE","object":"DATABASE","position":4000,"present_in":["6.4","6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"create a new database","purpose_zh":"","related":["alter-database","drop-database"],"slug":"create-database","source_rev":"a709ab85","synopsis":"CREATE DATABASE name\n[ WITH ] [ OWNER [=] user_name ]\n[ TEMPLATE [=] template ]\n[ ENCODING [=] encoding ]\n[ STRATEGY [=] strategy ]\n[ LOCALE [=] locale ]\n[ LC_COLLATE [=] lc_collate ]\n[ LC_CTYPE [=] lc_ctype ]\n[ BUILTIN_LOCALE [=] builtin_locale ]\n[ ICU_LOCALE [=] icu_locale ]\n[ ICU_RULES [=] icu_rules ]\n[ LOCALE_PROVIDER [=] locale_provider ]\n[ COLLATION_VERSION = collation_version ]\n[ TABLESPACE [=] tablespace_name ]\n[ ALLOW_CONNECTIONS [=] allowconn ]\n[ CONNECTION LIMIT [=] connlimit ]\n[ IS_TEMPLATE [=] istemplate ]\n[ OID [=] oid ]","verb":"CREATE"}},"Definition":{"Collection":"sql","Key":"create-database","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"create-database","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-CREATEDATABASE","file":"sql-createdatabase.html","lang":"en","name":"CREATE DATABASE","purpose":"create a new database","purpose_zh":"","related":["alter-database","drop-database"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e creates a new \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e database.\u003c/p\u003e\u003cp\u003eTo create a database, you must be a superuser or have the special \u003ccode class=\"literal\"\u003eCREATEDB\u003c/code\u003e privilege. See \u003ca href=\"/docs/18/sql-createrole.html\" title=\"CREATE ROLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE ROLE\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eBy default, the new database will be created by cloning the standard system database \u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e. A different template can be specified by writing \u003ccode class=\"literal\"\u003eTEMPLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e. In particular, by writing \u003ccode class=\"literal\"\u003eTEMPLATE template0\u003c/code\u003e, you can create a pristine database (one where no user-defined objects exist and where the system objects have not been altered) containing only the standard objects predefined by your version of \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e. This is useful if you wish to avoid copying any installation-local objects that might have been added to \u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of a database to create.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe role name of the user who will own the new database, or \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e to use the default (namely, the user executing the command). To create a database owned by another role, you must be able to \u003ccode class=\"literal\"\u003eSET ROLE\u003c/code\u003e to that role.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etemplate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the template from which to create the new database, or \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e to use the default template (\u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e).\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eencoding\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eCharacter set encoding to use in the new database. Specify a string constant (e.g., \u003ccode class=\"literal\"\u003e'SQL_ASCII'\u003c/code\u003e), or an integer encoding number, or \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e to use the default encoding (namely, the encoding of the template database). The character sets supported by the \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e server are described in \u003ca href=\"/docs/18/multibyte.html#MULTIBYTE-CHARSET-SUPPORTED\" title=\"23.3.1. Supported Character Sets\"\u003eSection 23.3.1\u003c/a\u003e. See below for additional restrictions.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003estrategy\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eStrategy to be used in creating the new database. If the \u003ccode class=\"literal\"\u003eWAL_LOG\u003c/code\u003e strategy is used, the database will be copied block by block and each block will be separately written to the write-ahead log. This is the most efficient strategy in cases where the template database is small, and therefore it is the default. The older \u003ccode class=\"literal\"\u003eFILE_COPY\u003c/code\u003e strategy is also available. This strategy writes a small record to the write-ahead log for each tablespace used by the target database. Each such record represents copying an entire directory to a new location at the filesystem level. While this does reduce the write-ahead log volume substantially, especially if the template database is large, it also forces the system to perform a checkpoint both before and after the creation of the new database. In some situations, this may have a noticeable negative impact on overall system performance. The \u003ccode class=\"literal\"\u003eFILE_COPY\u003c/code\u003e strategy is affected by the \u003ca href=\"/docs/18/runtime-config-resource.html#GUC-FILE-COPY-METHOD\"\u003efile_copy_method\u003c/a\u003e setting.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSets the default collation order and character classification in the new database. Collation affects the sort order applied to strings, e.g., in queries with \u003ccode class=\"literal\"\u003eORDER BY\u003c/code\u003e, as well as the order used in indexes on text columns. Character classification affects the categorization of characters, e.g., lower, upper, and digit. Also sets the associated aspects of the operating system environment, \u003ccode class=\"literal\"\u003eLC_COLLATE\u003c/code\u003e and \u003ccode class=\"literal\"\u003eLC_CTYPE\u003c/code\u003e. The default is the same setting as the template database. See \u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-LIBC\" title=\"23.2.2.3.1. libc Collations\"\u003eSection 23.2.2.3.1\u003c/a\u003e and \u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-ICU\" title=\"23.2.2.3.2. ICU Collations\"\u003eSection 23.2.2.3.2\u003c/a\u003e for details.\u003c/p\u003e\u003cp\u003eCan be overridden by setting \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LC-COLLATE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_collate\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e, \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LC-CTYPE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_ctype\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e, \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-BUILTIN-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e, or \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-ICU-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_locale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e individually.\u003c/p\u003e\u003cp\u003eIf \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e is \u003ccode class=\"literal\"\u003ebuiltin\u003c/code\u003e, then \u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e or \u003cem class=\"replaceable\"\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e must be specified and set to either \u003ccode class=\"literal\"\u003eC\u003c/code\u003e, \u003ccode class=\"literal\"\u003eC.UTF-8\u003c/code\u003e, or \u003ccode class=\"literal\"\u003ePG_UNICODE_FAST\u003c/code\u003e.\u003c/p\u003e\u003cdiv class=\"tip\"\u003e\u003ch3\u003eTip\u003c/h3\u003e\u003cp\u003eThe other locale settings \u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-MESSAGES\"\u003elc_messages\u003c/a\u003e, \u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-MONETARY\"\u003elc_monetary\u003c/a\u003e, \u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-NUMERIC\"\u003elc_numeric\u003c/a\u003e, and \u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-TIME\"\u003elc_time\u003c/a\u003e are not fixed per database and are not set by this command. If you want to make them the default for a specific database, you can use \u003ccode class=\"literal\"\u003eALTER DATABASE ... SET\u003c/code\u003e.\u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_collate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSets \u003ccode class=\"literal\"\u003eLC_COLLATE\u003c/code\u003e in the database server's operating system environment. The default is the setting of \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e if specified, otherwise the same setting as the template database. See below for additional restrictions.\u003c/p\u003e\u003cp\u003eIf \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e is \u003ccode class=\"literal\"\u003elibc\u003c/code\u003e, also sets the default collation order to use in the new database, overriding the setting \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_ctype\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSets \u003ccode class=\"literal\"\u003eLC_CTYPE\u003c/code\u003e in the database server's operating system environment. The default is the setting of \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e if specified, otherwise the same setting as the template database. See below for additional restrictions.\u003c/p\u003e\u003cp\u003eIf \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e is \u003ccode class=\"literal\"\u003elibc\u003c/code\u003e, also sets the default character classification to use in the new database, overriding the setting \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies the builtin provider locale for the database default collation order and character classification, overriding the setting \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e. The \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003elocale provider\u003c/a\u003e must be \u003ccode class=\"literal\"\u003ebuiltin\u003c/code\u003e. The default is the setting of \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e if specified; otherwise the same setting as the template database.\u003c/p\u003e\u003cp\u003eThe locales available for the \u003ccode class=\"literal\"\u003ebuiltin\u003c/code\u003e provider are \u003ccode class=\"literal\"\u003eC\u003c/code\u003e, \u003ccode class=\"literal\"\u003eC.UTF-8\u003c/code\u003e and \u003ccode class=\"literal\"\u003ePG_UNICODE_FAST\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_locale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies the ICU locale (see \u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-ICU\" title=\"23.2.2.3.2. ICU Collations\"\u003eSection 23.2.2.3.2\u003c/a\u003e) for the database default collation order and character classification, overriding the setting \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e. The \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003elocale provider\u003c/a\u003e must be ICU. The default is the setting of \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e if specified; otherwise the same setting as the template database.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_rules\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies additional collation rules to customize the behavior of the default collation of this database. This is supported for ICU only. See \u003ca href=\"/docs/18/collation.html#ICU-TAILORING-RULES\" title=\"23.2.3.4. ICU Tailoring Rules\"\u003eSection 23.2.3.4\u003c/a\u003e for details.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies the provider to use for the default collation in this database. Possible values are \u003ccode class=\"literal\"\u003ebuiltin\u003c/code\u003e, \u003ccode class=\"literal\"\u003eicu\u003c/code\u003e (if the server was built with ICU support) or \u003ccode class=\"literal\"\u003elibc\u003c/code\u003e. By default, the provider is the same as that of the \u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-TEMPLATE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etemplate\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e. See \u003ca href=\"/docs/18/locale.html#LOCALE-PROVIDERS\" title=\"23.1.4. Locale Providers\"\u003eSection 23.1.4\u003c/a\u003e for details.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation_version\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies the collation version string to store with the database. Normally, this should be omitted, which will cause the version to be computed from the actual version of the database collation as provided by the operating system. This option is intended to be used by \u003ccode class=\"command\"\u003epg_upgrade\u003c/code\u003e for copying the version from an existing installation.\u003c/p\u003e\u003cp\u003eSee also \u003ca href=\"/docs/18/sql-alterdatabase.html\" title=\"ALTER DATABASE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER DATABASE\u003c/span\u003e\u003c/a\u003e for how to handle database collation version mismatches.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the tablespace that will be associated with the new database, or \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e to use the template database's tablespace. This tablespace will be the default tablespace used for objects created in this database. See \u003ca href=\"/docs/18/sql-createtablespace.html\" title=\"CREATE TABLESPACE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLESPACE\u003c/span\u003e\u003c/a\u003e for more information.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eallowconn\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf false then no one can connect to this database. The default is true, allowing connections (except as restricted by other mechanisms, such as \u003ccode class=\"literal\"\u003eGRANT\u003c/code\u003e/\u003ccode class=\"literal\"\u003eREVOKE CONNECT\u003c/code\u003e).\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003econnlimit\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eHow many concurrent connections can be made to this database. -1 (the default) means no limit.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eistemplate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf true, then this database can be cloned by any user with \u003ccode class=\"literal\"\u003eCREATEDB\u003c/code\u003e privileges; if false (the default), then only superusers or the owner of the database can clone it.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eoid\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe object identifier to be used for the new database. If this parameter is not specified, \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e will choose a suitable OID automatically. This parameter is primarily intended for internal use by \u003cspan class=\"application\"\u003epg_upgrade\u003c/span\u003e, and only \u003cspan class=\"application\"\u003epg_upgrade\u003c/span\u003e can specify a value less than 16384.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003eOptional parameters can be written in any order, not only the order illustrated above.\u003c/p\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e cannot be executed inside a transaction block.\u003c/p\u003e\u003cp\u003eErrors along the line of \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003ecould not initialize database directory\u003c/span\u003e”\u003c/span\u003e are most likely related to insufficient permissions on the data directory, a full disk, or other file system problems.\u003c/p\u003e\u003cp\u003eUse \u003ca href=\"/docs/18/sql-dropdatabase.html\" title=\"DROP DATABASE\"\u003e\u003ccode class=\"command\"\u003eDROP DATABASE\u003c/code\u003e\u003c/a\u003e to remove a database.\u003c/p\u003e\u003cp\u003eThe program \u003ca href=\"/docs/18/app-createdb.html\" title=\"createdb\"\u003e\u003cspan class=\"refentrytitle\"\u003e\u003cspan class=\"application\"\u003ecreatedb\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e is a wrapper program around this command, provided for convenience.\u003c/p\u003e\u003cp\u003eDatabase-level configuration parameters (set via \u003ca href=\"/docs/18/sql-alterdatabase.html\" title=\"ALTER DATABASE\"\u003e\u003ccode class=\"command\"\u003eALTER DATABASE\u003c/code\u003e\u003c/a\u003e) and database-level permissions (set via \u003ca href=\"/docs/18/sql-grant.html\" title=\"GRANT\"\u003e\u003ccode class=\"command\"\u003eGRANT\u003c/code\u003e\u003c/a\u003e) are not copied from the template database.\u003c/p\u003e\u003cp\u003eAlthough it is possible to copy a database other than \u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e by specifying its name as the template, this is not (yet) intended as a general-purpose \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e\u003ccode class=\"command\"\u003eCOPY DATABASE\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e facility. The principal limitation is that no other sessions can be connected to the template database while it is being copied. \u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e will fail if any other connection exists when it starts; otherwise, new connections to the template database are locked out until \u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e completes. See \u003ca href=\"/docs/18/manage-ag-templatedbs.html\" title=\"22.3. Template Databases\"\u003eSection 22.3\u003c/a\u003e for more information.\u003c/p\u003e\u003cp\u003eThe character set encoding specified for the new database must be compatible with the chosen locale settings (\u003ccode class=\"literal\"\u003eLC_COLLATE\u003c/code\u003e and \u003ccode class=\"literal\"\u003eLC_CTYPE\u003c/code\u003e). If the locale is \u003ccode class=\"literal\"\u003eC\u003c/code\u003e (or equivalently \u003ccode class=\"literal\"\u003ePOSIX\u003c/code\u003e), then all encodings are allowed, but for other locale settings there is only one encoding that will work properly. (On Windows, however, UTF-8 encoding can be used with any locale.) \u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e will allow superusers to specify \u003ccode class=\"literal\"\u003eSQL_ASCII\u003c/code\u003e encoding regardless of the locale settings, but this choice is deprecated and may result in misbehavior of character-string functions if data that is not encoding-compatible with the locale is stored in the database.\u003c/p\u003e\u003cp\u003eThe encoding and locale settings must match those of the template database, except when \u003ccode class=\"literal\"\u003etemplate0\u003c/code\u003e is used as template. This is because other databases might contain data that does not match the specified encoding, or might contain indexes whose sort ordering is affected by \u003ccode class=\"literal\"\u003eLC_COLLATE\u003c/code\u003e and \u003ccode class=\"literal\"\u003eLC_CTYPE\u003c/code\u003e. Copying such data would result in a database that is corrupt according to the new settings. \u003ccode class=\"literal\"\u003etemplate0\u003c/code\u003e, however, is known to not contain any data or indexes that would be affected.\u003c/p\u003e\u003cp\u003eThere is currently no option to use a database locale with nondeterministic comparisons (see \u003ca href=\"/docs/18/sql-createcollation.html\" title=\"CREATE COLLATION\"\u003e\u003ccode class=\"command\"\u003eCREATE COLLATION\u003c/code\u003e\u003c/a\u003e for an explanation). If this is needed, then per-column collations would need to be used.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eCONNECTION LIMIT\u003c/code\u003e option is only enforced approximately; if two new sessions start at about the same time when just one connection \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eslot\u003c/span\u003e”\u003c/span\u003e remains for the database, it is possible that both will fail. Also, the limit is not enforced against superusers or background worker processes.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eTo create a new database:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE DATABASE lusiadas;\n\u003c/pre\u003e\u003cp\u003eTo create a database \u003ccode class=\"literal\"\u003esales\u003c/code\u003e owned by user \u003ccode class=\"literal\"\u003esalesapp\u003c/code\u003e with a default tablespace of \u003ccode class=\"literal\"\u003esalesspace\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;\n\u003c/pre\u003e\u003cp\u003eTo create a database \u003ccode class=\"literal\"\u003emusic\u003c/code\u003e with a different locale:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE DATABASE music\n    LOCALE 'sv_SE.utf8'\n    TEMPLATE template0;\n\u003c/pre\u003e\u003cp\u003eIn this example, the \u003ccode class=\"literal\"\u003eTEMPLATE template0\u003c/code\u003e clause is required if the specified locale is different from the one in \u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e. (If it is not, then specifying the locale explicitly is redundant.)\u003c/p\u003e\u003cp\u003eTo create a database \u003ccode class=\"literal\"\u003emusic2\u003c/code\u003e with a different locale and a different character set encoding:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE DATABASE music2\n    LOCALE 'sv_SE.iso885915'\n    ENCODING LATIN9\n    TEMPLATE template0;\n\u003c/pre\u003e\u003cp\u003eThe specified locale and encoding settings must match, or an error will be reported.\u003c/p\u003e\u003cp\u003eNote that locale names are specific to the operating system, so that the above commands might not work in the same way everywhere.\u003c/p\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThere is no \u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e statement in the SQL standard. Databases are equivalent to catalogs, whose creation is implementation-defined.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-database/?v=18\" title=\"ALTER DATABASE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER DATABASE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-database/?v=18\" title=\"DROP DATABASE\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP DATABASE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"CREATE DATABASE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    [ WITH ] [ OWNER [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e ]\n           [ TEMPLATE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etemplate\u003c/code\u003e\u003c/em\u003e ]\n           [ ENCODING [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eencoding\u003c/code\u003e\u003c/em\u003e ]\n           [ STRATEGY [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003estrategy\u003c/code\u003e\u003c/em\u003e ]\n           [ LOCALE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e ]\n           [ LC_COLLATE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_collate\u003c/code\u003e\u003c/em\u003e ]\n           [ LC_CTYPE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_ctype\u003c/code\u003e\u003c/em\u003e ]\n           [ BUILTIN_LOCALE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e ]\n           [ ICU_LOCALE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_locale\u003c/code\u003e\u003c/em\u003e ]\n           [ ICU_RULES [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_rules\u003c/code\u003e\u003c/em\u003e ]\n           [ LOCALE_PROVIDER [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e ]\n           [ COLLATION_VERSION = \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation_version\u003c/code\u003e\u003c/em\u003e ]\n           [ TABLESPACE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n           [ ALLOW_CONNECTIONS [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eallowconn\u003c/code\u003e\u003c/em\u003e ]\n           [ CONNECTION LIMIT [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003econnlimit\u003c/code\u003e\u003c/em\u003e ]\n           [ IS_TEMPLATE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eistemplate\u003c/code\u003e\u003c/em\u003e ]\n           [ OID [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoid\u003c/code\u003e\u003c/em\u003e ]","synopsis_text":"CREATE DATABASE name\n[ WITH ] [ OWNER [=] user_name ]\n[ TEMPLATE [=] template ]\n[ ENCODING [=] encoding ]\n[ STRATEGY [=] strategy ]\n[ LOCALE [=] locale ]\n[ LC_COLLATE [=] lc_collate ]\n[ LC_CTYPE [=] lc_ctype ]\n[ BUILTIN_LOCALE [=] builtin_locale ]\n[ ICU_LOCALE [=] icu_locale ]\n[ ICU_RULES [=] icu_rules ]\n[ LOCALE_PROVIDER [=] locale_provider ]\n[ COLLATION_VERSION = collation_version ]\n[ TABLESPACE [=] tablespace_name ]\n[ ALLOW_CONNECTIONS [=] allowconn ]\n[ CONNECTION LIMIT [=] connlimit ]\n[ IS_TEMPLATE [=] istemplate ]\n[ OID [=] oid ]"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"create-database","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"CREATE DATABASE","Summary":"创建一个新数据库","BodyHTML":"\u003cpre\u003eCREATE DATABASE name\n[ WITH ] [ OWNER [=] user_name ]\n[ TEMPLATE [=] template ]\n[ ENCODING [=] encoding ]\n[ STRATEGY [=] strategy ]\n[ LOCALE [=] locale ]\n[ LC_COLLATE [=] lc_collate ]\n[ LC_CTYPE [=] lc_ctype ]\n[ BUILTIN_LOCALE [=] builtin_locale ]\n[ ICU_LOCALE [=] icu_locale ]\n[ ICU_RULES [=] icu_rules ]\n[ LOCALE_PROVIDER [=] locale_provider ]\n[ COLLATION_VERSION = collation_version ]\n[ TABLESPACE [=] tablespace_name ]\n[ ALLOW_CONNECTIONS [=] allowconn ]\n[ CONNECTION LIMIT [=] connlimit ]\n[ IS_TEMPLATE [=] istemplate ]\n[ OID [=] oid ]\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE DATABASE\u003c/code\u003e 创建一个新的 \u003cspan\u003ePostgreSQL\u003c/span\u003e 数据库。\u003c/p\u003e\u003cp\u003e要创建数据库，你必须是超级用户，或者拥有特殊的 \u003ccode\u003eCREATEDB\u003c/code\u003e 权限。见\u003ca href=\"/docs/18/sql-createrole.html\" title=\"CREATE ROLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE ROLE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e默认情况下，新数据库通过克隆标准系统数据库 \u003ccode\u003etemplate1\u003c/code\u003e 来创建。可以通过写成 \u003ccode\u003eTEMPLATE \u003cem\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e 指定其他模板。特别是，写成 \u003ccode\u003eTEMPLATE template0\u003c/code\u003e 时，可以创建一个纯净的数据库（其中不存在用户定义对象，系统对象也未被改动），它只包含你的 \u003cspan\u003ePostgreSQL\u003c/span\u003e 版本预定义的标准对象。如果你希望避免复制任何可能已添加到 \u003ccode\u003etemplate1\u003c/code\u003e 中的站点本地附加对象，这会很有用。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要创建的数据库名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将成为新数据库拥有者的用户角色名，或者指定 \u003ccode\u003eDEFAULT\u003c/code\u003e 以使用默认值（即执行该命令的用户）。要创建由其他角色拥有的数据库，你必须能够对该角色执行 \u003ccode\u003eSET ROLE\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003etemplate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e用于创建新数据库的模板名称，或者指定 \u003ccode\u003eDEFAULT\u003c/code\u003e 以使用默认模板（\u003ccode\u003etemplate1\u003c/code\u003e）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eencoding\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e新数据库要使用的字符集编码。可指定字符串常量（例如 \u003ccode\u003e\u0026#39;SQL_ASCII\u0026#39;\u003c/code\u003e）、整数编码编号，或者指定 \u003ccode\u003eDEFAULT\u003c/code\u003e 以使用默认编码（即模板数据库的编码）。\u003cspan\u003ePostgreSQL\u003c/span\u003e 服务器支持的字符集见\u003ca href=\"/docs/18/multibyte.html#MULTIBYTE-CHARSET-SUPPORTED\" rel=\"nofollow\"\u003e第 23.3.1 节\u003c/a\u003e。其他限制见下文。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003estrategy\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e创建新数据库时使用的策略。如果使用 \u003ccode\u003eWAL_LOG\u003c/code\u003e 策略，数据库将按块复制，并且每个块都会分别写入预写式日志。当模板数据库较小时，这是最高效的策略，因此也是默认策略。较早的 \u003ccode\u003eFILE_COPY\u003c/code\u003e 策略也可用。该策略会为目标数据库使用的每个表空间向预写式日志写入一条较小的记录，每条记录都表示在文件系统层面把整个目录复制到一个新位置。虽然这样能大幅减少预写式日志量，特别是在模板数据库很大时更是如此，但它也会迫使系统在创建新数据库之前和之后各执行一次检查点。在某些情况下，这可能会对整体系统性能产生明显的负面影响。\u003ccode\u003eFILE_COPY\u003c/code\u003e 策略受\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-FILE-COPY-METHOD\" rel=\"nofollow\"\u003efile_copy_method\u003c/a\u003e设置影响。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e设置新数据库中的默认排序规则和字符分类。排序规则会影响应用于字符串的排序顺序，例如带有 \u003ccode\u003eORDER BY\u003c/code\u003e 的查询，以及文本列索引中使用的顺序。字符分类会影响字符的类别划分，例如小写、大写和数字。它还会设置操作系统环境中相应的 \u003ccode\u003eLC_COLLATE\u003c/code\u003e 和 \u003ccode\u003eLC_CTYPE\u003c/code\u003e。默认值与模板数据库中的设置相同。详见\u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-LIBC\" rel=\"nofollow\"\u003e第 23.2.2.3.1 节\u003c/a\u003e和\u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-ICU\" rel=\"nofollow\"\u003e第 23.2.2.3.2 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e也可以通过分别设置\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LC-COLLATE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elc_collate\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e、\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LC-CTYPE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elc_ctype\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e、\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-BUILTIN-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e或\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-ICU-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003eicu_locale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e来覆盖它。\u003c/p\u003e\u003cp\u003e如果\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e是 \u003ccode\u003ebuiltin\u003c/code\u003e，则必须指定 \u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e 或 \u003cem\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e，并将其设为 \u003ccode\u003eC\u003c/code\u003e、\u003ccode\u003eC.UTF-8\u003c/code\u003e 或 \u003ccode\u003ePG_UNICODE_FAST\u003c/code\u003e之一。\u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e提示\u003c/h3\u003e\u003cp\u003e其他区域设置参数\u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-MESSAGES\" rel=\"nofollow\"\u003elc_messages\u003c/a\u003e、\u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-MONETARY\" rel=\"nofollow\"\u003elc_monetary\u003c/a\u003e、\u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-NUMERIC\" rel=\"nofollow\"\u003elc_numeric\u003c/a\u003e和\u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-TIME\" rel=\"nofollow\"\u003elc_time\u003c/a\u003e并不是按数据库固定的，也不会由此命令设置。如果你希望把它们设为某个特定数据库的默认值，可以使用 \u003ccode\u003eALTER DATABASE ... SET\u003c/code\u003e。\u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003elc_collate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e在数据库服务器的操作系统环境中设置 \u003ccode\u003eLC_COLLATE\u003c/code\u003e。默认值为\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置（如果已指定），否则与模板数据库中的设置相同。其他限制见下文。\u003c/p\u003e\u003cp\u003e如果\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e是 \u003ccode\u003elibc\u003c/code\u003e，它还会设置新数据库中使用的默认排序规则，从而覆盖\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003elc_ctype\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e在数据库服务器的操作系统环境中设置 \u003ccode\u003eLC_CTYPE\u003c/code\u003e。默认值为\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置（如果已指定），否则与模板数据库中的设置相同。其他限制见下文。\u003c/p\u003e\u003cp\u003e如果\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e是 \u003ccode\u003elibc\u003c/code\u003e，它还会设置新数据库中使用的默认字符分类，从而覆盖\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定用于数据库默认排序规则和字符分类的 builtin 提供程序区域设置，从而覆盖\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置。\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\" rel=\"nofollow\"\u003e区域设置提供程序\u003c/a\u003e必须是 \u003ccode\u003ebuiltin\u003c/code\u003e。默认值为\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置（如果已指定），否则与模板数据库中的设置相同。\u003c/p\u003e\u003cp\u003e\u003ccode\u003ebuiltin\u003c/code\u003e 提供程序可用的区域设置有 \u003ccode\u003eC\u003c/code\u003e、\u003ccode\u003eC.UTF-8\u003c/code\u003e 和 \u003ccode\u003ePG_UNICODE_FAST\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eicu_locale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定 ICU 区域设置（见\u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-ICU\" rel=\"nofollow\"\u003e第 23.2.2.3.2 节\u003c/a\u003e），用于数据库默认排序规则和字符分类，从而覆盖\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置。\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\" rel=\"nofollow\"\u003e区域设置提供程序\u003c/a\u003e必须是 ICU。默认值为\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置（如果已指定），否则与模板数据库中的设置相同。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eicu_rules\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定附加的排序规则，用以定制该数据库默认排序规则的行为。该选项仅支持 ICU。详见\u003ca href=\"/docs/18/collation.html#ICU-TAILORING-RULES\" rel=\"nofollow\"\u003e第 23.2.3.4 节\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定该数据库默认排序规则使用的提供程序。可能的值为 \u003ccode\u003ebuiltin\u003c/code\u003e、\u003ccode\u003eicu\u003c/code\u003e（如果服务器构建时启用了 ICU 支持）或 \u003ccode\u003elibc\u003c/code\u003e。默认情况下，该值与\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-TEMPLATE\" rel=\"nofollow\"\u003e\u003cem\u003e\u003ccode\u003etemplate\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的区域设置提供程序相同。详见\u003ca href=\"/docs/18/locale.html#LOCALE-PROVIDERS\" rel=\"nofollow\"\u003e第 23.1.4 节\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003ecollation_version\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定要与数据库一起存储的排序规则版本字符串。通常应省略此选项，这样系统会根据操作系统提供的数据库排序规则实际版本来计算该版本。此选项主要供 \u003ccode\u003epg_upgrade\u003c/code\u003e 使用，以便从现有安装中复制该版本。\u003c/p\u003e\u003cp\u003e另见\u003ca href=\"/docs/18/sql-alterdatabase.html\" title=\"ALTER DATABASE\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER DATABASE\u003c/span\u003e\u003c/a\u003e，了解如何处理数据库排序规则版本不匹配。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将与新数据库关联的表空间名称，或者指定 \u003ccode\u003eDEFAULT\u003c/code\u003e 以使用模板数据库的表空间。该表空间将成为在此数据库中创建对象时使用的默认表空间。详见\u003ca href=\"/docs/18/sql-createtablespace.html\" title=\"CREATE TABLESPACE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLESPACE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eallowconn\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果为 false，则任何人都不能连接到该数据库。默认值为 true，即允许连接（但仍受其他机制限制，例如 \u003ccode\u003eGRANT\u003c/code\u003e/\u003ccode\u003eREVOKE CONNECT\u003c/code\u003e）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003econnlimit\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e可对该数据库建立的并发连接数。-1（默认值）表示不受限制。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eistemplate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果为 true，则任何具有 \u003ccode\u003eCREATEDB\u003c/code\u003e 权限的用户都可以克隆该数据库；如果为 false（默认值），则只有超级用户或该数据库的拥有者可以克隆它。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eoid\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要用于新数据库的对象标识符。如果未指定此参数，\u003cspan\u003ePostgreSQL\u003c/span\u003e 将自动选择一个合适的 OID。此参数主要供 \u003cspan\u003epg_upgrade\u003c/span\u003e 内部使用，且只有 \u003cspan\u003epg_upgrade\u003c/span\u003e 才能指定小于 16384 的值。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e可选参数可以按任意顺序书写，不一定要按照上面的顺序。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE DATABASE\u003c/code\u003e 不能在事务块内执行。\u003c/p\u003e\u003cp\u003e形如\u003cspan\u003e“\u003cspan\u003ecould not initialize database directory\u003c/span\u003e”\u003c/span\u003e的错误，多半与数据目录权限不足、磁盘已满或其他文件系统问题有关。\u003c/p\u003e\u003cp\u003e使用 \u003ca href=\"/docs/18/sql-dropdatabase.html\" title=\"DROP DATABASE\" rel=\"nofollow\"\u003e\u003ccode\u003eDROP DATABASE\u003c/code\u003e\u003c/a\u003e 删除数据库。\u003c/p\u003e\u003cp\u003e程序\u003ca href=\"/docs/18/app-createdb.html\" title=\"createdb\" rel=\"nofollow\"\u003e\u003cspan\u003e\u003cspan\u003ecreatedb\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e是这个命令的一个包装器程序，为方便使用而提供。\u003c/p\u003e\u003cp\u003e数据库级配置参数（通过 \u003ca href=\"/docs/18/sql-alterdatabase.html\" title=\"ALTER DATABASE\" rel=\"nofollow\"\u003e\u003ccode\u003eALTER DATABASE\u003c/code\u003e\u003c/a\u003e 设置）以及数据库级权限（通过 \u003ca href=\"/docs/18/sql-grant.html\" title=\"GRANT\" rel=\"nofollow\"\u003e\u003ccode\u003eGRANT\u003c/code\u003e\u003c/a\u003e 设置）都不会从模板数据库中复制。\u003c/p\u003e\u003cp\u003e尽管可以通过把某个数据库名指定为模板，从而复制它而不是复制 \u003ccode\u003etemplate1\u003c/code\u003e，但这（至少目前）并不打算作为一种通用的\u003cspan\u003e“\u003cspan\u003e\u003ccode\u003eCOPY DATABASE\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e功能。主要限制是，在复制模板数据库期间，不能有任何其他会话连接到该数据库。\u003ccode\u003eCREATE DATABASE\u003c/code\u003e 在启动时如果发现存在任何其他连接，就会失败；否则，在 \u003ccode\u003eCREATE DATABASE\u003c/code\u003e 完成之前，将阻止对模板数据库建立新的连接。详见\u003ca href=\"/docs/18/manage-ag-templatedbs.html\" rel=\"nofollow\"\u003e第 22.3 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e为新数据库指定的字符集编码必须与所选区域设置（\u003ccode\u003eLC_COLLATE\u003c/code\u003e 和 \u003ccode\u003eLC_CTYPE\u003c/code\u003e）兼容。如果区域设置为 \u003ccode\u003eC\u003c/code\u003e（或等价的 \u003ccode\u003ePOSIX\u003c/code\u003e），则允许所有编码；但对于其他区域设置，只有一种编码能够正常工作。（不过，在 Windows 上，UTF-8 编码可与任何区域设置一起使用。）\u003ccode\u003eCREATE DATABASE\u003c/code\u003e 允许超级用户不考虑区域设置而指定 \u003ccode\u003eSQL_ASCII\u003c/code\u003e 编码，但这种选择已弃用；如果数据库中存储了与该区域设置不兼容编码的数据，字符串函数的行为可能会出错。\u003c/p\u003e\u003cp\u003e编码和区域设置必须与模板数据库的设置一致，除非使用 \u003ccode\u003etemplate0\u003c/code\u003e 作为模板。这是因为其他数据库可能包含与指定编码不匹配的数据，或者包含排序顺序会受 \u003ccode\u003eLC_COLLATE\u003c/code\u003e 和 \u003ccode\u003eLC_CTYPE\u003c/code\u003e 影响的索引。复制这样的数据会导致数据库在新设置下被视为损坏。不过，已知 \u003ccode\u003etemplate0\u003c/code\u003e 不包含任何会受此影响的数据或索引。\u003c/p\u003e\u003cp\u003e目前还没有选项可以让数据库区域设置使用非确定性比较（解释见 \u003ca href=\"/docs/18/sql-createcollation.html\" title=\"CREATE COLLATION\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE COLLATION\u003c/code\u003e\u003c/a\u003e）。如果需要这样做，则必须使用逐列排序规则。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eCONNECTION LIMIT\u003c/code\u003e 选项只是近似地被强制执行；如果两个新会话几乎同时启动，而该数据库只剩下一个连接\u003cspan\u003e“\u003cspan\u003e槽\u003c/span\u003e”\u003c/span\u003e，则两者都可能失败。此外，该限制对超级用户或后台工作进程无效。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e要创建一个新数据库：\u003c/p\u003e\u003cpre\u003eCREATE DATABASE lusiadas;\n\u003c/pre\u003e\u003cp\u003e要创建一个由用户 \u003ccode\u003esalesapp\u003c/code\u003e 拥有、默认表空间为 \u003ccode\u003esalesspace\u003c/code\u003e 的数据库 \u003ccode\u003esales\u003c/code\u003e：\u003c/p\u003e\u003cpre\u003eCREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;\n\u003c/pre\u003e\u003cp\u003e要创建一个使用不同区域设置的数据库 \u003ccode\u003emusic\u003c/code\u003e：\u003c/p\u003e\u003cpre\u003eCREATE DATABASE music\n    LOCALE \u0026#39;sv_SE.utf8\u0026#39;\n    TEMPLATE template0;\n\u003c/pre\u003e\u003cp\u003e在这个示例中，如果指定的区域设置与 \u003ccode\u003etemplate1\u003c/code\u003e 中的不同，则必须使用 \u003ccode\u003eTEMPLATE template0\u003c/code\u003e 子句。（如果相同，那么显式指定区域设置就是多余的。）\u003c/p\u003e\u003cp\u003e要创建一个使用不同区域设置和不同字符集编码的数据库 \u003ccode\u003emusic2\u003c/code\u003e：\u003c/p\u003e\u003cpre\u003eCREATE DATABASE music2\n    LOCALE \u0026#39;sv_SE.iso885915\u0026#39;\n    ENCODING LATIN9\n    TEMPLATE template0;\n\u003c/pre\u003e\u003cp\u003e指定的区域设置和编码设置必须匹配，否则会报错。\u003c/p\u003e\u003cp\u003e请注意，区域设置名称是特定于操作系统的，因此上述命令未必在所有环境中都以相同方式工作。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003eSQL 标准中没有 \u003ccode\u003eCREATE DATABASE\u003c/code\u003e 语句。数据库相当于目录，而目录的创建由实现定义。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/alter-database/?v=18\" title=\"ALTER DATABASE\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER DATABASE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-database/?v=18\" title=\"DROP DATABASE\" rel=\"nofollow\"\u003e\u003cspan\u003eDROP DATABASE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"97128d67227c85000231ca56114f9280b207e308126e7b4a13f9d89139e9e21f","Payload":{"purpose_zh":"创建一个新数据库","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e 创建一个新的 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 数据库。\u003c/p\u003e\u003cp\u003e要创建数据库，你必须是超级用户，或者拥有特殊的 \u003ccode class=\"literal\"\u003eCREATEDB\u003c/code\u003e 权限。见\u003ca href=\"/docs/18/sql-createrole.html\" title=\"CREATE ROLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE ROLE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e默认情况下，新数据库通过克隆标准系统数据库 \u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e 来创建。可以通过写成 \u003ccode class=\"literal\"\u003eTEMPLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e 指定其他模板。特别是，写成 \u003ccode class=\"literal\"\u003eTEMPLATE template0\u003c/code\u003e 时，可以创建一个纯净的数据库（其中不存在用户定义对象，系统对象也未被改动），它只包含你的 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 版本预定义的标准对象。如果你希望避免复制任何可能已添加到 \u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e 中的站点本地附加对象，这会很有用。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要创建的数据库名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将成为新数据库拥有者的用户角色名，或者指定 \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e 以使用默认值（即执行该命令的用户）。要创建由其他角色拥有的数据库，你必须能够对该角色执行 \u003ccode class=\"literal\"\u003eSET ROLE\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etemplate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e用于创建新数据库的模板名称，或者指定 \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e 以使用默认模板（\u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eencoding\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e新数据库要使用的字符集编码。可指定字符串常量（例如 \u003ccode class=\"literal\"\u003e'SQL_ASCII'\u003c/code\u003e）、整数编码编号，或者指定 \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e 以使用默认编码（即模板数据库的编码）。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 服务器支持的字符集见\u003ca href=\"/docs/18/multibyte.html#MULTIBYTE-CHARSET-SUPPORTED\" title=\"23.3.1. 支持的字符集\"\u003e第 23.3.1 节\u003c/a\u003e。其他限制见下文。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003estrategy\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e创建新数据库时使用的策略。如果使用 \u003ccode class=\"literal\"\u003eWAL_LOG\u003c/code\u003e 策略，数据库将按块复制，并且每个块都会分别写入预写式日志。当模板数据库较小时，这是最高效的策略，因此也是默认策略。较早的 \u003ccode class=\"literal\"\u003eFILE_COPY\u003c/code\u003e 策略也可用。该策略会为目标数据库使用的每个表空间向预写式日志写入一条较小的记录，每条记录都表示在文件系统层面把整个目录复制到一个新位置。虽然这样能大幅减少预写式日志量，特别是在模板数据库很大时更是如此，但它也会迫使系统在创建新数据库之前和之后各执行一次检查点。在某些情况下，这可能会对整体系统性能产生明显的负面影响。\u003ccode class=\"literal\"\u003eFILE_COPY\u003c/code\u003e 策略受\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-FILE-COPY-METHOD\"\u003efile_copy_method\u003c/a\u003e设置影响。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e设置新数据库中的默认排序规则和字符分类。排序规则会影响应用于字符串的排序顺序，例如带有 \u003ccode class=\"literal\"\u003eORDER BY\u003c/code\u003e 的查询，以及文本列索引中使用的顺序。字符分类会影响字符的类别划分，例如小写、大写和数字。它还会设置操作系统环境中相应的 \u003ccode class=\"literal\"\u003eLC_COLLATE\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eLC_CTYPE\u003c/code\u003e。默认值与模板数据库中的设置相同。详见\u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-LIBC\" title=\"23.2.2.3.1. libc 排序规则\"\u003e第 23.2.2.3.1 节\u003c/a\u003e和\u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-ICU\" title=\"23.2.2.3.2. ICU 排序规则\"\u003e第 23.2.2.3.2 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e也可以通过分别设置\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LC-COLLATE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_collate\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e、\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LC-CTYPE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_ctype\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e、\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-BUILTIN-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e或\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-ICU-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_locale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e来覆盖它。\u003c/p\u003e\u003cp\u003e如果\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e是 \u003ccode class=\"literal\"\u003ebuiltin\u003c/code\u003e，则必须指定 \u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e 或 \u003cem class=\"replaceable\"\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e，并将其设为 \u003ccode class=\"literal\"\u003eC\u003c/code\u003e、\u003ccode class=\"literal\"\u003eC.UTF-8\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003ePG_UNICODE_FAST\u003c/code\u003e之一。\u003c/p\u003e\u003cdiv class=\"tip\"\u003e\u003ch3\u003e提示\u003c/h3\u003e\u003cp\u003e其他区域设置参数\u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-MESSAGES\"\u003elc_messages\u003c/a\u003e、\u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-MONETARY\"\u003elc_monetary\u003c/a\u003e、\u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-NUMERIC\"\u003elc_numeric\u003c/a\u003e和\u003ca href=\"/docs/18/runtime-config-client.html#GUC-LC-TIME\"\u003elc_time\u003c/a\u003e并不是按数据库固定的，也不会由此命令设置。如果你希望把它们设为某个特定数据库的默认值，可以使用 \u003ccode class=\"literal\"\u003eALTER DATABASE ... SET\u003c/code\u003e。\u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_collate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e在数据库服务器的操作系统环境中设置 \u003ccode class=\"literal\"\u003eLC_COLLATE\u003c/code\u003e。默认值为\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置（如果已指定），否则与模板数据库中的设置相同。其他限制见下文。\u003c/p\u003e\u003cp\u003e如果\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e是 \u003ccode class=\"literal\"\u003elibc\u003c/code\u003e，它还会设置新数据库中使用的默认排序规则，从而覆盖\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_ctype\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e在数据库服务器的操作系统环境中设置 \u003ccode class=\"literal\"\u003eLC_CTYPE\u003c/code\u003e。默认值为\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置（如果已指定），否则与模板数据库中的设置相同。其他限制见下文。\u003c/p\u003e\u003cp\u003e如果\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e是 \u003ccode class=\"literal\"\u003elibc\u003c/code\u003e，它还会设置新数据库中使用的默认字符分类，从而覆盖\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定用于数据库默认排序规则和字符分类的 builtin 提供程序区域设置，从而覆盖\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置。\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003e区域设置提供程序\u003c/a\u003e必须是 \u003ccode class=\"literal\"\u003ebuiltin\u003c/code\u003e。默认值为\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置（如果已指定），否则与模板数据库中的设置相同。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003ebuiltin\u003c/code\u003e 提供程序可用的区域设置有 \u003ccode class=\"literal\"\u003eC\u003c/code\u003e、\u003ccode class=\"literal\"\u003eC.UTF-8\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003ePG_UNICODE_FAST\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_locale\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定 ICU 区域设置（见\u003ca href=\"/docs/18/collation.html#COLLATION-MANAGING-CREATE-ICU\" title=\"23.2.2.3.2. ICU 排序规则\"\u003e第 23.2.2.3.2 节\u003c/a\u003e），用于数据库默认排序规则和字符分类，从而覆盖\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置。\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER\"\u003e区域设置提供程序\u003c/a\u003e必须是 ICU。默认值为\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-LOCALE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的设置（如果已指定），否则与模板数据库中的设置相同。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_rules\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定附加的排序规则，用以定制该数据库默认排序规则的行为。该选项仅支持 ICU。详见\u003ca href=\"/docs/18/collation.html#ICU-TAILORING-RULES\" title=\"23.2.3.4. ICU 定制规则\"\u003e第 23.2.3.4 节\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定该数据库默认排序规则使用的提供程序。可能的值为 \u003ccode class=\"literal\"\u003ebuiltin\u003c/code\u003e、\u003ccode class=\"literal\"\u003eicu\u003c/code\u003e（如果服务器构建时启用了 ICU 支持）或 \u003ccode class=\"literal\"\u003elibc\u003c/code\u003e。默认情况下，该值与\u003ca href=\"/docs/18/sql-createdatabase.html#CREATE-DATABASE-TEMPLATE\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etemplate\u003c/code\u003e\u003c/em\u003e\u003c/a\u003e的区域设置提供程序相同。详见\u003ca href=\"/docs/18/locale.html#LOCALE-PROVIDERS\" title=\"23.1.4. 区域设置提供程序\"\u003e第 23.1.4 节\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation_version\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定要与数据库一起存储的排序规则版本字符串。通常应省略此选项，这样系统会根据操作系统提供的数据库排序规则实际版本来计算该版本。此选项主要供 \u003ccode class=\"command\"\u003epg_upgrade\u003c/code\u003e 使用，以便从现有安装中复制该版本。\u003c/p\u003e\u003cp\u003e另见\u003ca href=\"/docs/18/sql-alterdatabase.html\" title=\"ALTER DATABASE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER DATABASE\u003c/span\u003e\u003c/a\u003e，了解如何处理数据库排序规则版本不匹配。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将与新数据库关联的表空间名称，或者指定 \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e 以使用模板数据库的表空间。该表空间将成为在此数据库中创建对象时使用的默认表空间。详见\u003ca href=\"/docs/18/sql-createtablespace.html\" title=\"CREATE TABLESPACE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLESPACE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eallowconn\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果为 false，则任何人都不能连接到该数据库。默认值为 true，即允许连接（但仍受其他机制限制，例如 \u003ccode class=\"literal\"\u003eGRANT\u003c/code\u003e/\u003ccode class=\"literal\"\u003eREVOKE CONNECT\u003c/code\u003e）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003econnlimit\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e可对该数据库建立的并发连接数。-1（默认值）表示不受限制。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eistemplate\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果为 true，则任何具有 \u003ccode class=\"literal\"\u003eCREATEDB\u003c/code\u003e 权限的用户都可以克隆该数据库；如果为 false（默认值），则只有超级用户或该数据库的拥有者可以克隆它。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eoid\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要用于新数据库的对象标识符。如果未指定此参数，\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 将自动选择一个合适的 OID。此参数主要供 \u003cspan class=\"application\"\u003epg_upgrade\u003c/span\u003e 内部使用，且只有 \u003cspan class=\"application\"\u003epg_upgrade\u003c/span\u003e 才能指定小于 16384 的值。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e可选参数可以按任意顺序书写，不一定要按照上面的顺序。\u003c/p\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e 不能在事务块内执行。\u003c/p\u003e\u003cp\u003e形如\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003ecould not initialize database directory\u003c/span\u003e”\u003c/span\u003e的错误，多半与数据目录权限不足、磁盘已满或其他文件系统问题有关。\u003c/p\u003e\u003cp\u003e使用 \u003ca href=\"/docs/18/sql-dropdatabase.html\" title=\"DROP DATABASE\"\u003e\u003ccode class=\"command\"\u003eDROP DATABASE\u003c/code\u003e\u003c/a\u003e 删除数据库。\u003c/p\u003e\u003cp\u003e程序\u003ca href=\"/docs/18/app-createdb.html\" title=\"createdb\"\u003e\u003cspan class=\"refentrytitle\"\u003e\u003cspan class=\"application\"\u003ecreatedb\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e是这个命令的一个包装器程序，为方便使用而提供。\u003c/p\u003e\u003cp\u003e数据库级配置参数（通过 \u003ca href=\"/docs/18/sql-alterdatabase.html\" title=\"ALTER DATABASE\"\u003e\u003ccode class=\"command\"\u003eALTER DATABASE\u003c/code\u003e\u003c/a\u003e 设置）以及数据库级权限（通过 \u003ca href=\"/docs/18/sql-grant.html\" title=\"GRANT\"\u003e\u003ccode class=\"command\"\u003eGRANT\u003c/code\u003e\u003c/a\u003e 设置）都不会从模板数据库中复制。\u003c/p\u003e\u003cp\u003e尽管可以通过把某个数据库名指定为模板，从而复制它而不是复制 \u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e，但这（至少目前）并不打算作为一种通用的\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e\u003ccode class=\"command\"\u003eCOPY DATABASE\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e功能。主要限制是，在复制模板数据库期间，不能有任何其他会话连接到该数据库。\u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e 在启动时如果发现存在任何其他连接，就会失败；否则，在 \u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e 完成之前，将阻止对模板数据库建立新的连接。详见\u003ca href=\"/docs/18/manage-ag-templatedbs.html\" title=\"22.3. 模板数据库\"\u003e第 22.3 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e为新数据库指定的字符集编码必须与所选区域设置（\u003ccode class=\"literal\"\u003eLC_COLLATE\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eLC_CTYPE\u003c/code\u003e）兼容。如果区域设置为 \u003ccode class=\"literal\"\u003eC\u003c/code\u003e（或等价的 \u003ccode class=\"literal\"\u003ePOSIX\u003c/code\u003e），则允许所有编码；但对于其他区域设置，只有一种编码能够正常工作。（不过，在 Windows 上，UTF-8 编码可与任何区域设置一起使用。）\u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e 允许超级用户不考虑区域设置而指定 \u003ccode class=\"literal\"\u003eSQL_ASCII\u003c/code\u003e 编码，但这种选择已弃用；如果数据库中存储了与该区域设置不兼容编码的数据，字符串函数的行为可能会出错。\u003c/p\u003e\u003cp\u003e编码和区域设置必须与模板数据库的设置一致，除非使用 \u003ccode class=\"literal\"\u003etemplate0\u003c/code\u003e 作为模板。这是因为其他数据库可能包含与指定编码不匹配的数据，或者包含排序顺序会受 \u003ccode class=\"literal\"\u003eLC_COLLATE\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eLC_CTYPE\u003c/code\u003e 影响的索引。复制这样的数据会导致数据库在新设置下被视为损坏。不过，已知 \u003ccode class=\"literal\"\u003etemplate0\u003c/code\u003e 不包含任何会受此影响的数据或索引。\u003c/p\u003e\u003cp\u003e目前还没有选项可以让数据库区域设置使用非确定性比较（解释见 \u003ca href=\"/docs/18/sql-createcollation.html\" title=\"CREATE COLLATION\"\u003e\u003ccode class=\"command\"\u003eCREATE COLLATION\u003c/code\u003e\u003c/a\u003e）。如果需要这样做，则必须使用逐列排序规则。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eCONNECTION LIMIT\u003c/code\u003e 选项只是近似地被强制执行；如果两个新会话几乎同时启动，而该数据库只剩下一个连接\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e槽\u003c/span\u003e”\u003c/span\u003e，则两者都可能失败。此外，该限制对超级用户或后台工作进程无效。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e要创建一个新数据库：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE DATABASE lusiadas;\n\u003c/pre\u003e\u003cp\u003e要创建一个由用户 \u003ccode class=\"literal\"\u003esalesapp\u003c/code\u003e 拥有、默认表空间为 \u003ccode class=\"literal\"\u003esalesspace\u003c/code\u003e 的数据库 \u003ccode class=\"literal\"\u003esales\u003c/code\u003e：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;\n\u003c/pre\u003e\u003cp\u003e要创建一个使用不同区域设置的数据库 \u003ccode class=\"literal\"\u003emusic\u003c/code\u003e：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE DATABASE music\n    LOCALE 'sv_SE.utf8'\n    TEMPLATE template0;\n\u003c/pre\u003e\u003cp\u003e在这个示例中，如果指定的区域设置与 \u003ccode class=\"literal\"\u003etemplate1\u003c/code\u003e 中的不同，则必须使用 \u003ccode class=\"literal\"\u003eTEMPLATE template0\u003c/code\u003e 子句。（如果相同，那么显式指定区域设置就是多余的。）\u003c/p\u003e\u003cp\u003e要创建一个使用不同区域设置和不同字符集编码的数据库 \u003ccode class=\"literal\"\u003emusic2\u003c/code\u003e：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE DATABASE music2\n    LOCALE 'sv_SE.iso885915'\n    ENCODING LATIN9\n    TEMPLATE template0;\n\u003c/pre\u003e\u003cp\u003e指定的区域设置和编码设置必须匹配，否则会报错。\u003c/p\u003e\u003cp\u003e请注意，区域设置名称是特定于操作系统的，因此上述命令未必在所有环境中都以相同方式工作。\u003c/p\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003eSQL 标准中没有 \u003ccode class=\"command\"\u003eCREATE DATABASE\u003c/code\u003e 语句。数据库相当于目录，而目录的创建由实现定义。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-database/?v=18\" title=\"ALTER DATABASE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER DATABASE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-database/?v=18\" title=\"DROP DATABASE\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP DATABASE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"CREATE DATABASE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    [ WITH ] [ OWNER [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e ]\n           [ TEMPLATE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etemplate\u003c/code\u003e\u003c/em\u003e ]\n           [ ENCODING [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eencoding\u003c/code\u003e\u003c/em\u003e ]\n           [ STRATEGY [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003estrategy\u003c/code\u003e\u003c/em\u003e ]\n           [ LOCALE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale\u003c/code\u003e\u003c/em\u003e ]\n           [ LC_COLLATE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_collate\u003c/code\u003e\u003c/em\u003e ]\n           [ LC_CTYPE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003elc_ctype\u003c/code\u003e\u003c/em\u003e ]\n           [ BUILTIN_LOCALE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ebuiltin_locale\u003c/code\u003e\u003c/em\u003e ]\n           [ ICU_LOCALE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_locale\u003c/code\u003e\u003c/em\u003e ]\n           [ ICU_RULES [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eicu_rules\u003c/code\u003e\u003c/em\u003e ]\n           [ LOCALE_PROVIDER [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003elocale_provider\u003c/code\u003e\u003c/em\u003e ]\n           [ COLLATION_VERSION = \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation_version\u003c/code\u003e\u003c/em\u003e ]\n           [ TABLESPACE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n           [ ALLOW_CONNECTIONS [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eallowconn\u003c/code\u003e\u003c/em\u003e ]\n           [ CONNECTION LIMIT [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003econnlimit\u003c/code\u003e\u003c/em\u003e ]\n           [ IS_TEMPLATE [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eistemplate\u003c/code\u003e\u003c/em\u003e ]\n           [ OID [=] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoid\u003c/code\u003e\u003c/em\u003e ]","synopsis_text":"CREATE DATABASE name\n[ WITH ] [ OWNER [=] user_name ]\n[ TEMPLATE [=] template ]\n[ ENCODING [=] encoding ]\n[ STRATEGY [=] strategy ]\n[ LOCALE [=] locale ]\n[ LC_COLLATE [=] lc_collate ]\n[ LC_CTYPE [=] lc_ctype ]\n[ BUILTIN_LOCALE [=] builtin_locale ]\n[ ICU_LOCALE [=] icu_locale ]\n[ ICU_RULES [=] icu_rules ]\n[ LOCALE_PROVIDER [=] locale_provider ]\n[ COLLATION_VERSION = collation_version ]\n[ TABLESPACE [=] tablespace_name ]\n[ ALLOW_CONNECTIONS [=] allowconn ]\n[ CONNECTION LIMIT [=] connlimit ]\n[ IS_TEMPLATE [=] istemplate ]\n[ OID [=] oid ]"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","6.4","6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
