CREATE_DATABASE(7pg13)
π CREATE_DATABASE π
okay bestie, welcome to the CREATE DATABASE page β this is the PostgreSQL 13.23 Documentation version, so like, that's the exact vibe we're working with, no cap. Sit down, hydrate, let's go π«Ά
β¨ NAME β¨
CREATE_DATABASE β create a new database, literally that's the whole gig π
πΈ SYNOPSIS πΈ
CREATE DATABASE name
[ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ LOCALE [=] locale ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ TABLESPACE [=] tablespace_name ]
[ ALLOW_CONNECTIONS [=] allowconn ]
[ CONNECTION LIMIT [=] connlimit ]
[ IS_TEMPLATE [=] istemplate ]
like, look at all those optional brackets β she's flexible, she's cute, she's giving main character energy π
π¦ DESCRIPTION π¦
So CREATE DATABASE creates a new PostgreSQL database. That's it. That's the tweet. β¨
But lowkey, there's a whole permission situation: to create a database you must be a superuser or have the special CREATEDB privilege. Check out CREATE ROLE (CREATE_ROLE(7)) for that. π
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 here's the iconic trick: by writing TEMPLATE template0, you get 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 PostgreSQL. Obsessed π this is useful if you wanna avoid copying any installation-local objects that might have been added to template1.
π PARAMETERS π
nameβ the name of the database you're creating. yes, she needs a name, she's a whole person now π₯Ή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 a direct or indirect member of that role, or be a superuser. iykyk πtemplateβ the name of the template to create the new database from, orDEFAULTto use the default template (template1). π§encodingβ the 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 β yes there are plot twists, keep reading πlocaleβ this is the shortcut for settingLC_COLLATEandLC_CTYPEat once. If you specify this, you cannot specify either of those parameters. pick a lane bestie π
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β collation order (LC_COLLATE) to use in the new database. This affects the sort order applied to strings, e.g., in queries withORDER BY, as well as the order used in indexes on text columns. The default is to use the collation order of the template database. See below for additional restrictions. π€lc_ctypeβ character classification (LC_CTYPE) to use in the new database. This affects the categorization of characters, e.g., lower, upper and digit. The default is to use the character classification of the template database. See below for additional restrictions. π‘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 info. π¦allowconnβ iffalsethen no one can connect to this database. The default istrue, allowing connections (except as restricted by other mechanisms, such asGRANT/REVOKE CONNECT). πͺconnlimitβ how many concurrent connections can be made to this database.-1(the default) means no limit. let her breathe π¬οΈistemplateβ iftrue, then this database can be cloned by any user withCREATEDBprivileges; iffalse(the default), then only superusers or the owner of the database can clone it. π―
Also, optional parameters can be written in any order, not only the order illustrated above. she's not picky like that β¨
π₯ NOTES π₯
CREATE DATABASE cannot be executed inside a transaction block. periodt. π«
Errors along the line of "could not initialize database directory" are most likely related to insufficient permissions on the data directory, a full disk, or other file system problems. so like, check your disk before you spiral π
Use DROP DATABASE (DROP_DATABASE(7)) to remove a database. bye bitch π
The program createdb(1) is a wrapper program around this command, provided for convenience. cute lil shortcut π
Database-level configuration parameters (set via ALTER DATABASE (ALTER_DATABASE(7))) and database-level permissions (set via GRANT(7)) are not copied from the template database. nope, they stay home π
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's 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 info. π
Now, the encoding drama π¬ 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. ngl, don't do it π
The encoding and locale settings must match those of the template database, except when template0 is used as template. Why? 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. she's a clean slate, she's safe πΈ
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. rules for thee etc etc π
π 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.) πΆ
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. π₯
Note that locale names are specific to the operating system, so that the above commands might not work in the same way everywhere. π
β COMPATIBILITY β
There is no CREATE DATABASE statement in the SQL standard. Databases are equivalent to catalogs, whose creation is implementation-defined. the standard said "not my problem" π€·ββοΈ
π SEE ALSO π
ALTER DATABASE(ALTER_DATABASE(7))DROP DATABASE(DROP_DATABASE(7))
and that's the whole page bestie!! πβ¨ππ¦πΈπ π«Ά
kiss kiss, this was PostgreSQL 13.23, dated 2025 β go forth and create databases responsibly, mwah ππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-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