|PostgreSQL Reference Manual - Volume 1 - SQL Language Reference|
by The PostgreSQL Global Development Group
Paperback (6"x9"), 716 pages
RRP £32.00 ($49.95)
Sales of this book support the PostgreSQL project! Get a printed copy>>>
12.33 CREATE DATABASE
CREATE DATABASE -- create a new database
CREATE DATABASE name [ [ WITH ] [ OWNER [=] dbowner ] [ TEMPLATE [=] template ] [ ENCODING [=] encoding ] [ TABLESPACE [=] tablespace ] [ CONNECTION LIMIT [=] connlimit ] ]
CREATE DATABASE creates a new
To create a database, you must be a superuser or have the special
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
privilege can only create databases owned by themselves.
By default, the new database will be created by cloning the standard
template1. A different template can be
specified by writing
name. In particular,
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 may have been added to
- The name of a database to create.
The name of the database user who will own the new database,
DEFAULTto use the default (namely, the user executing the command).
The name of the template from which to create the new database,
DEFAULTto use the default template (
Character set encoding to use in the new database. Specify
a string constant (e.g.,
'SQL_ASCII'), or an integer encoding number, or
DEFAULTto use the default encoding (namely, the encoding of the template database). The character sets supported by the PostgreSQL server are described in Volume 3: Supported Character Sets.
The name of the tablespace that will be associated with the
new database, or
DEFAULTto use the template database's tablespace. This tablespace will be the default tablespace used for objects created in this database. See
CREATE TABLESPACEfor more information.
- How many concurrent connections can be made to this database. -1 (the default) means no limit.
Optional parameters can be written in any order, not only the order illustrated above.
CREATE DATABASE cannot be executed inside a transaction
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.
DROP DATABASE to remove a database.
createdb is a
wrapper program around this command, provided for convenience.
Although it is possible to copy a database other than
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.
DATABASE will fail if any other connection exists when it starts;
otherwise, new connections to the template database are locked out
CREATE DATABASE completes.
See Volume 3: Template Databases for more information.
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.
To create a new database:
CREATE DATABASE lusiadas;
To create a database
sales owned by user
with a default tablespace of
CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
To create a database
music which supports the ISO-8859-1
CREATE DATABASE music ENCODING 'LATIN1';
There is no
CREATE DATABASE statement in the SQL
standard. Databases are equivalent to catalogs, whose creation is
|ISBN 0954612027||PostgreSQL Reference Manual - Volume 1 - SQL Language Reference||See the print edition|