Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
CREATE DATABASE
Prev UpSQL CommandsHome Next

CREATE DATABASE

CREATE DATABASE — create a new database

Synopsis

CREATE DATABASEname    [ WITH ] [ OWNER [=]user_name ]           [ TEMPLATE [=]template ]           [ ENCODING [=]encoding ]           [ LC_COLLATE [=]lc_collate[@provider] ]           [ LC_CTYPE [=]lc_ctype ]           [ TABLESPACE [=]tablespace_name ]           [ ALLOW_CONNECTIONS [=]allowconn ]           [ CONNECTION LIMIT [=]connlimit ]           [ IS_TEMPLATE [=]istemplate ]

Description

CREATE DATABASE creates a newPostgres Pro database.

To create a database, you must be a superuser or have the specialCREATEDB privilege. SeeCREATE ROLE.

By default, the new database will be created by cloning the standard system databasetemplate1. A different template can be specified by writingTEMPLATEname. In particular, by writingTEMPLATE template0, you can create a virgin database containing only the standard objects predefined by your version ofPostgres Pro. This is useful if you wish to avoid copying any installation-local objects that might have been added totemplate1.

Parameters

name

The name of a database to create.

user_name

The role name of the user who will own the new database, orDEFAULT to use the default (namely, the user executing the command). To create a database owned by another role, you must be a direct or indirect member of that role, or be a superuser.

template

The name of the template from which to create the new database, orDEFAULT 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, orDEFAULT to use the default encoding (namely, the encoding of the template database). The character sets supported by thePostgres Pro server are described inSection 22.3.1. See below for additional restrictions.

lc_collate[@provider]

Collation order (LC_COLLATE) to use in the new database. This affects the sort order applied to strings, e.g., in queries with ORDER BY, as well as the order used in indexes on text columns. The default is to use the collation order of the template database. Optionally, you can specify the provider of the default collation after the@ symbol, as explained inSection 22.2.2. Supported values areicu andlibc. See below for additional restrictions.

lc_ctype

Character classification (LC_CTYPE) to use in the new database. This affects the categorization of characters, e.g., lower, upper and digit. The default is to use the character classification of the template database. See below for additional restrictions.

tablespace_name

The name of the tablespace that will be associated with the new database, orDEFAULT to use the template database's tablespace. This tablespace will be the default tablespace used for objects created in this database. SeeCREATE TABLESPACE for more information.

allowconn

If false then no one can connect to this database. The default is true, allowing connections (except as restricted by other mechanisms, such asGRANT/REVOKE CONNECT).

connlimit

How many concurrent connections can be made to this database. -1 (the default) means no limit.

istemplate

If true, then this database can be cloned by any user withCREATEDB privileges; if false (the default), then only superusers or the owner of the database can clone it.

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 ofcould not initialize database directory are most likely related to insufficient permissions on the data directory, a full disk, or other file system problems.

UseDROP DATABASE to remove a database.

The programcreatedb is a wrapper program around this command, provided for convenience.

Database-level configuration parameters (set viaALTER DATABASE) and database-level permissions (set viaGRANT) are not copied from the template database.

Although it is possible to copy a database other thantemplate1 by specifying its name as the template, this is not (yet) intended as a general-purposeCOPY 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 untilCREATE DATABASE completes. SeeSection 21.3 for more information.

The character set encoding specified for the new database must be compatible with the chosen locale settings (LC_COLLATE andLC_CTYPE). If the locale isC (or equivalentlyPOSIX), then all encodings are allowed, but for other locale settings there is only one encoding that will work properly. (On Windows, however, UTF-8 encoding can be used with any locale.)CREATE DATABASE will allow superusers to specifySQL_ASCII encoding regardless of the locale settings, 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 encoding and locale settings must match those of the template database, except whentemplate0 is used as template. This is because other databases might contain data that does not match the specified encoding, or might contain indexes whose sort ordering is affected byLC_COLLATE andLC_CTYPE. Copying such data would result in a database that is corrupt according to the new settings.template0, however, is known to not contain any data or indexes that would be affected.

TheCONNECTION LIMIT option is only enforced approximately; if two new sessions start at about the same time when just one connectionslot remains for the database, it is possible that both will fail. Also, the limit is not enforced against superusers or background worker processes.

Examples

To create a new database:

CREATE DATABASE lusiadas;

To create a databasesales owned by usersalesapp with a default tablespace ofsalesspace:

CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;

To create a databasemusic with a different locale:

CREATE DATABASE music    LC_COLLATE 'sv_SE.utf8' LC_CTYPE 'sv_SE.utf8'    TEMPLATE template0;

In this example, theTEMPLATE template0 clause is required if the specified locale is different from the one intemplate1. (If it is not, then specifying the locale explicitly is redundant.)

To create a databasemusic2 with a different locale and a different character set encoding:

CREATE DATABASE music2    LC_COLLATE 'sv_SE.iso885915' LC_CTYPE 'sv_SE.iso885915'    ENCODING LATIN9    TEMPLATE template0;

The specified locale and encoding settings must match, or an error will be reported.

Note that locale names are specific to the operating system, so that the above commands might not work in the same way everywhere.

Compatibility

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


Prev Up Next
CREATE CONVERSION Home CREATE DOMAIN
epubpdf
Go to Postgres Pro Standard 11
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp