CREATE DATABASE name [ WITH [ LOCATION = 'dbpath' ] [ TEMPLATE = template ] [ ENCODING = encoding ] ] |
The name of a database to create.
An alternative file system location in which to store the new database, specified as a string literal; or DEFAULT to use the default location.
Name of template from which to create the new database, or DEFAULT to use the default template (template1).
Multibyte encoding method to use in the new database. Specify a string literal name (for example, 'SQL_ASCII'), or an integer encoding number, or DEFAULT to use the default encoding.
Message returned if the command completes successfully.
You must have the special CREATEDB privilege to create databases. See CREATE USER.
This occurs if a database with the name specified already exists.
The database location dbpath cannot contain single quotes. This is required so that the shell commands that create the database directory can execute safely.
If you have a transaction block in progress you cannot call CREATE DATABASE. You must finish the transaction first.
These are most likely related to insufficient permissions on the data directory, a full disk, or other file system problems. The user under which the database server is running must have access to the location.
CREATE DATABASE creates a new PostgreSQL database. The creator becomes the owner of the new database.
An alternate 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 command. For more information, refer to the Red Hat Database Administrator and User's Guide.
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 PGDATA2.) If the server is compiled with ALLOW_ABSOLUTE_DBPATHS (not so by default), absolute path names, as identified by a leading slash (for example, /var/lib/pgsql/data), are allowed as well.
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, if your server was compiled with multibyte encoding support. When not specified, it defaults to the encoding used by the selected template database.
Use DROP DATABASE to remove a database.
The program createdb is a shell script wrapper around this command, provided for convenience. For more information, refer to the Red Hat Database Administrator and User's Guide.
There are security and data integrity issues involved with using alternate database locations specified with absolute path names, and by default only an environment variable known to the backend may be specified for an alternate location.
Although it is possible to copy a database other than template1 by specifying its name as the template, this is not intended as a general-purpose COPY DATABASE facility. Databases used as templates should be treated as read-only. See the Red Hat Database Administrator and User's Guide for more information.