CREATE DATABASE(7)

NAME
CREATE DATABASE - create a new database

SYNOPSIS

CREATE DATABASE name
    [ [ WITH ] [ OWNER [=] dbowner ]
           [ LOCATION [=] 'dbpath' ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ] ]

DESCRIPTION

CREATE DATABASE creates a new PostgreSQL database.

To create a database, you must be a superuser or have the special CREATEDB privilege. See CREATE USER [create_user(7)].

Normally, the creator becomes the owner of the new database. Superusers can create databases owned by other users 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.

An alternative location can be specified in order to, for example, store the database on a different disk. The path must have been prepared with the initlocation [initlocation(1)] command.

If the path name does not contain a slash, it is interpreted as an environment variable name, which must be known to the server process. This way the database administrator can exercise control over locations in which databases can be created. (A customary choice is, e.g., PGDATA2.) If the server is compiled with ALLOW_ABSOLUTE_DBPATHS (not so by default), absolute path names, as identified by a leading slash (e.g., /usr/local/pgsql/data), are allowed as well. In either case, the final path name must be absolute and must not contain any single quotes.

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 may have been added to template1.

The optional encoding parameter allows selection of the database encoding. When not specified, it defaults to the encoding used by the selected template database.

PARAMETERS

name
    The name of a database to create.

dbowner
    The name of the database user who will own the new database, or DEFAULT to use the default (namely, the user executing the command).

dbpath
    An alternate file-system location in which to store the new database, specified as a string literal; or DEFAULT to use the default location.

template
    The name of the template from which to create the new database, or DEFAULT to 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, or DEFAULT to use the default encoding.

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

NOTES

CREATE 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. When using an alternate location, the user under which the database server is running must have access to the location.

Use DROP DATABASE [drop_database(7)] to remove a database.

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

There are security issues involved with using alternate database locations specified with absolute path names; this is why the feature is not enabled by default. See the chapter called ``Managing Databases'' in the documentation for more information.

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. We recommend that databases used as templates be treated as read-only. See the chapter called ``Managing Databases'' in the documentation for more information.

EXAMPLES

To create a new database:

CREATE DATABASE lusiadas;

To create a new database in an alternate area ~/private_db, execute the following from the shell:

mkdir private_db
initlocation ~/private_db

Then execute the following from within a psql session:

CREATE DATABASE elsewhere WITH LOCATION '/home/olly/private_db';

COMPATIBILITY

There is no CREATE DATABASE statement in the SQL standard. Databases are equivalent to catalogs, whose creation is implementation-defined.