CREATE_DATABASE(7pg17)
π CREATE_DATABASE π β 7pg17, PostgreSQL 17.11 docs
πΈ NAME πΈ
CREATE_DATABASE β create a brand-new PostgreSQL database, no cap π
β¨ SYNOPSIS β¨
CREATE DATABASE name
[ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ STRATEGY [=] strategy ]
[ LOCALE [=] locale ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ BUILTIN_LOCALE [=] builtin_locale ]
[ ICU_LOCALE [=] icu_locale ]
[ ICU_RULES [=] icu_rules ]
[ LOCALE_PROVIDER [=] locale_provider ]
[ COLLATION_VERSION = collation_version ]
[ TABLESPACE [=] tablespace_name ]
[ ALLOW_CONNECTIONS [=] allowconn ]
[ CONNECTION LIMIT [=] connlimit ]
[ IS_TEMPLATE [=] istemplate ]
[ OID [=] oid ]
π¦ DESCRIPTION π¦
Okay so CREATE DATABASE literally creates a new PostgreSQL database, bestie. Iconic.
To create a database, you gotta be a superuser or have the special CREATEDB privilege. See CREATE ROLE (CREATE_ROLE(7)) for that whole situation.
By default, the new database gets created by cloning the standard system database template1. If you want a different template, just write TEMPLATE name. And look β if you write TEMPLATE template0, you get a pristine database. Like, 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 PostgreSQL. That's lowkey useful if you wanna avoid copying any installation-local objects that might have been added to template1. Slay.
π PARAMETERS π
nameβ the name of a database to create. That's it, that's the whole vibe.user_nameβ the role name of the user who will own the new database, orDEFAULTto use the default (namely, the user executing the command). To create a database owned by another role, you must be able toSET ROLEto that role. Periodt.templateβ the name of the template from which to create the new database, orDEFAULTto use the default template (template1).encodingβ character set encoding to use in the new database. Specify a string constant (e.g.,'SQL_ASCII'), or an integer encoding number, orDEFAULTto use the default encoding (namely, the encoding of the template database). The character sets supported by the PostgreSQL server are described in Section 23.3.1. See below for additional restrictions.strategyβ strategy to be used in creating the new database. If theWAL_LOGstrategy 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 olderFILE_COPYstrategy 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. So like, know what you're doing before you pick that one πlocaleβ sets the default collation order and character classification in the new database. Collation affects the sort order applied to strings, e.g., in queries withORDER BY, 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,LC_COLLATEandLC_CTYPE. The default is the same setting as the template database. See Section 23.2.2.3.1 and Section 23.2.2.3.2 for details.
Can be overridden by setting lc_collate, lc_ctype, builtin_locale, or icu_locale individually.
If locale_provider is builtin, then locale or builtin_locale must be specified and set to either C or C.UTF-8.
Tip: The other locale settingslc_messages,lc_monetary,lc_numeric, andlc_timeare 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 useALTER DATABASE ... SET.
lc_collateβ setsLC_COLLATEin the database server's operating system environment. The default is the setting oflocaleif specified, otherwise the same setting as the template database. See below for additional restrictions.
If locale_provider is libc, also sets the default collation order to use in the new database, overriding the setting locale.
lc_ctypeβ setsLC_CTYPEin the database server's operating system environment. The default is the setting oflocaleif specified, otherwise the same setting as the template database. See below for additional restrictions.
If locale_provider is libc, also sets the default character classification to use in the new database, overriding the setting locale.
builtin_localeβ specifies the builtin provider locale for the database default collation order and character classification, overriding the settinglocale. The locale provider must bebuiltin. The default is the setting oflocaleif specified; otherwise the same setting as the template database.
The locales available for the builtin provider are C and C.UTF-8.
icu_localeβ specifies the ICU locale (see Section 23.2.2.3.2) for the database default collation order and character classification, overriding the settinglocale. The locale provider must be ICU. The default is the setting oflocaleif specified; otherwise the same setting as the template database.icu_rulesβ specifies additional collation rules to customize the behavior of the default collation of this database. This is supported for ICU only. See Section 23.2.3.4 for details. Fancy fancy β¨locale_providerβ specifies the provider to use for the default collation in this database. Possible values arebuiltin,icu(if the server was built with ICU support) orlibc. By default, the provider is the same as that of the template. See Section 23.1.4 for details.collation_versionβ specifies 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 bypg_upgradefor copying the version from an existing installation.
See also ALTER DATABASE (ALTER_DATABASE(7)) for how to handle database collation version mismatches. Important bestie, don't ignore that.
tablespace_nameβ the name of the tablespace that will be associated with the new database, orDEFAULTto use the template database's tablespace. This tablespace will be the default tablespace used for objects created in this database. SeeCREATE TABLESPACE (CREATE_TABLESPACE(7))for more information.allowconnβ if false then no one can connect to this database. The default is true, allowing connections (except as restricted by other mechanisms, such asGRANT/REVOKE CONNECT). So like, true is the default, bestie.connlimitβ how many concurrent connections can be made to this database.-1(the default) means no limit. The way that-1means "go off, no cap" here πistemplateβ if true, then this database can be cloned by any user withCREATEDBprivileges; if false (the default), then only superusers or the owner of the database can clone it.oidβ the object identifier to be used for the new database. If this parameter is not specified, PostgreSQL will choose a suitable OID automatically. This parameter is primarily intended for internal use bypg_upgrade, and onlypg_upgradecan specify a value less than16384. That number is not a suggestion, bestie π
Optional parameters can be written in any order, not only the order illustrated above.
π NOTES π
Okay so first vibe check, bestie: CREATE DATABASE cannot be executed inside a transaction block. Like, it is simply not that girl in there. β¨
If you see errors along the line of βcould not initialize database directoryβ, that's most likely about insufficient permissions on the data directory, a full disk, or other file system problems. Not me diagnosing your whole filesystem, but alsoβ¦ yes. π
Use DROP DATABASE to remove a database. That's the cleanup era.
The program createdb(1) is a wrapper program around this command, provided for convenience. Cute little shortcut, no cap. π
Database-level configuration parameters (set via ALTER DATABASE) and database-level permissions (set via GRANT) are not copied from the template database. So don't assume they come along for the ride, because they do not. π
Although it is possible to copy a database other than template1 by specifying its name as the template, this is not (yet) intended as a general-purpose βCOPY DATABASEβ facility. The principal limitation is that no other sessions can be connected to the template database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; otherwise, new connections to the template database are locked out until CREATE DATABASE completes. See Section 22.3 for more information. π«Ά
The character set encoding specified for the new database must be compatible with the chosen locale settings (LC_COLLATE and LC_CTYPE). If the locale is C (or equivalently POSIX), 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.) CREATE DATABASE will allow superusers to specify SQL_ASCII 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. β οΈ
The encoding and locale settings must match those of the template database, except when template0 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 LC_COLLATE and LC_CTYPE. Copying such data would result in a database that is corrupt according to the new settings. template0, however, is known to not contain any data or indexes that would be affected. So likeβ¦ template0 is the safe clean-girl option here. π
There is currently no option to use a database locale with nondeterministic comparisons (see CREATE COLLATION for an explanation). If this is needed, then per-column collations would need to be used. That's the whole tea. π΅
The CONNECTION LIMIT option is only enforced approximately; if two new sessions start at about the same time when just one connection βslotβ remains for the database, it is possible that both will fail. Also, the limit is not enforced against superusers or background worker processes. So it's giving "approximately, bestie," not "military-grade bouncer at the club." π
β¨ EXAMPLES β¨
To create a new database:
CREATE DATABASE lusiadas;
To create a database sales owned by user salesapp with a default tablespace of salesspace:
CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
To create a database music with a different locale:
CREATE DATABASE music
LOCALE 'sv_SE.utf8'
TEMPLATE template0;
In this example, the TEMPLATE template0 clause is required if the specified locale is different from the one in template1. (If it is not, then specifying the locale explicitly is redundant.) Like, don't do extra for no reason. πΈ
To create a database music2 with a different locale and a different character set encoding:
CREATE DATABASE music2
LOCALE 'sv_SE.iso885915'
ENCODING LATIN9
TEMPLATE template0;
The specified locale and encoding settings must match, or an error will be reported. Fr fr, they have to match. π₯
Note that locale names are specific to the operating system, so that the above commands might not work in the same way everywhere. Your OS said "my names are my names," and honestly? Periodt. π
π COMPATIBILITY π
There is no CREATE DATABASE statement in the SQL standard. Databases are equivalent to catalogs, whose creation is implementation-defined. So the SQL standard is not giving us a CREATE DATABASE moment, and that's just how it is. π
π¦ SEE ALSO π¦
ALTER DATABASE(ALTER_DATABASE(7))DROP DATABASE(DROP_DATABASE(7))
Okay that's the whole lore, bestie β this was CREATE DATABASE(7) from PostgreSQL 17.11, dated 2026. Signing off with a lil mwah πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms