CREATE DATABASE
The CREATE DATABASE
command creates a new database.
Description
To create a database, you must be a superuser or have the special CREATEDB
privilege. 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 pristine database (one where no user-defined objects exist and where the system objects have not been altered) 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.
Usage
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 ] ]
Parameter | Description |
---|---|
name |
The name of a database to create |
user_name |
The role name of the user who will own the new database, or |
template |
The name of the template from which to create the new database, or |
encoding |
Character set encoding to use in the new database. Specify a string constant (e.g., |
locale |
This is a shortcut for setting |
lc_collate |
Collation order ( |
lc_ctype |
Character classification ( |
tablespace_name |
The name of the tablespace that will be associated with the new database, or |
allowconn |
If |
connlimit |
How many concurrent connections can be made to this database. -1 (the default) means no limit |
istemplate |
If |
Example
To create a new database:
CREATE DATABASE customers;
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 is 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.