![]() | ![]() | ![]() | ![]() |
CREATE DATABASESynopsisCREATE DATABASE name
[ [ WITH ] [ OWNER [=] dbowner ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ TABLESPACE [=] tablespace ]
[ CONNECTION LIMIT [=] connlimit ] ]DescriptionCREATE DATABASE creates a new PostgreSQL database. To create a database, you must be a superuser or have the special CREATEDB privilege. See CREATE USER. Normally, the creator becomes the owner of the new database. Superusers can create databases owned by other users, by using the OWNER clause. They can even create databases owned by users with no special privileges. Non-superusers with CREATEDB privilege can only create databases owned by themselves. By default, the new database will be created by cloning the standard system database template1. A different template can be specified by writing TEMPLATE name. In particular, by writing TEMPLATE template0, you can create a virgin database containing only the standard objects predefined by your version of PostgreSQL. This is useful if you wish to avoid copying any installation-local objects that might have been added to template1. Parameters
Optional parameters can be written in any order, not only the order illustrated above. NotesCREATE DATABASE cannot be executed inside a transaction block. 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 is a wrapper program around this command, provided for convenience. 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 20.3 for more information. Any character set encoding specified for the new database must be compatible with the server's LC_CTYPE locale setting. If LC_CTYPE is C (or equivalently POSIX), then all encodings are allowed, but for other locale settings there is only one encoding that will work properly, and so the apparent freedom to specify an encoding is illusory if you didn't initialize the database cluster in C locale. CREATE DATABASE will allow superusers to specify SQL_ASCII encoding regardless of the locale setting, 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 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. ExamplesTo 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 which supports the ISO-8859-1 character set: CREATE DATABASE music ENCODING 'LATIN1';
|
||||||||||||