CREATE_DATABASE(7pg14)

πŸ“¦ version 14.24, in opensuse-leap 16.0

✨ CREATE_DATABASE ✨

Hey bestie! So this is the PostgreSQL 14.24 documentation for CREATE_DATABASE, and it's literally your new bestie for spinning up fresh databases. Let's get into it πŸ’…


🌸 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 ]

πŸ¦‹ DESCRIPTION πŸ¦‹

So CREATE DATABASE creates a new PostgreSQL database. That's the whole vibe. πŸŽ€

To actually do this, you need to be a superuser or have the special CREATEDB privilege. See CREATE ROLE (CREATE_ROLE(7)).

By default, your new database gets cloned from the standard system database template1. You can pick a different template by writing TEMPLATE name. But highkey the most iconic move is TEMPLATE template0β€”that gives you a pristine database (no user-defined objects, no altered system objects), containing only the standard objects predefined by your version of PostgreSQL. Perfect if you wanna avoid copying any installation-local objects that might have been added to template1. Obsessed with that for you πŸ’–


πŸŽ€ PARAMETERS πŸŽ€

  • name β€” The name of the database you're creating. Simple as that.
  • user_name β€” The role name of the user who'll own the new database, or DEFAULT to use the default (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. No cap.
  • template β€” The name of the template to create the new database from, or DEFAULT to use the default template (template1).
  • encoding β€” Character set encoding to use in the new database. Specify a string constant (like 'SQL_ASCII'), or an integer encoding number, or DEFAULT to use the default encoding (the encoding of the template database). The character sets supported by the PostgreSQL server are described in Section 24.3.1. See below for additional restrictions.
  • locale β€” This is a shortcut for setting LC_COLLATE and LC_CTYPE at once. If you specify this, you cannot specify either of those parameters. Periodt.
πŸ’‘ Tip: The other locale settings lc_messages, lc_monetary, lc_numeric, and lc_time 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 ALTER DATABASE ... SET.
  • lc_collate β€” Collation order (LC_COLLATE) to use in the new database. This affects the sort order applied to strings, like in queries with ORDER BY, and also 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β€”lower, upper, digit, etc. The default is to use the character classification of the template database. See below for additional restrictions.
  • tablespace_name β€” The name of the tablespace associated with the new database, or DEFAULT to use the template database's tablespace. This tablespace will be the default tablespace used for objects created in this database. See CREATE TABLESPACE (CREATE_TABLESPACE(7)) for more info.
  • allowconn β€” If false, then no one can connect to this database. The default is true, allowing connections (except as restricted by other mechanisms, like GRANT/REVOKE CONNECT).
  • connlimit β€” How many concurrent connections can be made to this database. -1 (the default) means no limit.
  • istemplate β€” If true, then this database can be cloned by any user with CREATEDB privileges; if false (the default), then only superusers or the owner of the database can clone it.

Optional parameters can be written in any order, not only the order illustrated above. Slay.


πŸ’œ NOTES πŸ’œ

CREATE DATABASE cannot be executed inside a transaction block. Not me finding that out the hard way 😭

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.

Use DROP DATABASE to remove a database.

The program createdb(1) is a wrapper program around this command, provided for convenience.

Database-level configuration parameters (set via ALTER DATABASE) and database-level permissions (set via GRANT) are not copied from the template database. Just so you know.

Although it's 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 23.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. Big yikes.

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 doesn't 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.

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's possible that both will fail. Also, the limit is not enforced against superusers or background worker processes. RIP to precision, but we move. πŸ’€


🌈 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's 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 those commands might not work in the same way everywhere. iykyk 🫢

⭐ COMPATIBILITY ⭐

There is no CREATE DATABASE statement in the SQL standard. Databases are equivalent to catalogs, whose creation is implementation-defined. We're built different, fr fr.


πŸ‘‘ SEE ALSO πŸ‘‘


Okay bestie, that's the whole page! You're now officially a database-creating icon. Go forth and CREATE DATABASE like the main character you are πŸ’–βœ¨

Signing off from the PostgreSQL 14.24 docs, dated 2026 β€” catch you on the flip side! πŸ¦‹πŸŒΈ
πŸ“¦ where this page comes from