バージョンごとのドキュメント一覧

CREATE DATABASE

CREATE DATABASE <refpurpose>create a new database</refpurpose> — 新しいデータベースを作成する

概要

CREATE DATABASE name
    [ WITH ] [ OWNER [=] user_name ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ]
           [ STRATEGY [=] strategy ] ]
           [ LOCALE [=] locale ]
           [ LC_COLLATE [=] lc_collate ]
           [ LC_CTYPE [=] lc_ctype ]
           [ ICU_LOCALE [=] icu_locale ]
           [ ICU_RULES [=] icu_rules ]
           [ LOCALE_PROVIDER [=] locale_provider ]
           [ COLLATION_VERSION = collation_version ]
           [ TABLESPACE [=] tablespace_name ]
           [ ALLOW_CONNECTIONS [=] allowconn ]
           [ CONNECTION LIMIT [=] connlimit ]
           [ IS_TEMPLATE [=] istemplate ]
           [ OID [=] oid ]

説明

<title>Description</title>

<command>CREATE DATABASE</command> creates a new <productname>PostgreSQL</productname> database. CREATE DATABASEは新しいPostgreSQLデータベースを作成します。

To create a database, you must be a superuser or have the special <literal>CREATEDB</literal> privilege. See <xref linkend="sql-createrole"/>. データベースを作成するには、スーパーユーザ、もしくはCREATEDBという特別な権限を持つユーザである必要があります。 CREATE ROLEを参照してください。

By default, the new database will be created by cloning the standard system database <literal>template1</literal>. A different template can be specified by writing <literal>TEMPLATE <replaceable class="parameter">name</replaceable></literal>. In particular, by writing <literal>TEMPLATE template0</literal>, 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 <productname>PostgreSQL</productname>. This is useful if you wish to avoid copying any installation-local objects that might have been added to <literal>template1</literal>. デフォルトでは、新しいデータベースは標準システムデータベースtemplate1を複製することによって作成されます。 他のテンプレートを指定するには、TEMPLATE nameと記述します。 特に、TEMPLATE template0と記述することで、そのバージョンのPostgreSQLによって定義済みの標準オブジェクトのみを持つ、(そこではユーザ定義オブジェクトは存在せず、システムオブジェクトは変更されていない)初期状態のデータベースを作ることができます。 これは、template1に追加した独自オブジェクトをコピーしたくない場合に便利です。

パラメータ

<title>Parameters</title>
name #

The name of a database to create. 作成するデータベースの名前です。

user_name #

The role name of the user who will own the new database, or <literal>DEFAULT</literal> to use the default (namely, the user executing the command). To create a database owned by another role, you must be able to <literal>SET ROLE</literal> to that role. 新しいデータベースを所有するユーザのロール名です。 デフォルト設定(つまり、コマンドを実行したユーザ)を使用する場合はDEFAULTと指定します。 他のロールによって所有されるデータベースを作成するためには、そのロールに対してSET ROLEができなければなりません。

template #

The name of the template from which to create the new database, or <literal>DEFAULT</literal> to use the default template (<literal>template1</literal>). 新しいデータベースの作成元となるテンプレートの名前です。 デフォルトテンプレート(template1)を使う場合は、DEFAULTと指定します。

encoding #

Character set encoding to use in the new database. Specify a string constant (e.g., <literal>'SQL_ASCII'</literal>), or an integer encoding number, or <literal>DEFAULT</literal> to use the default encoding (namely, the encoding of the template database). The character sets supported by the <productname>PostgreSQL</productname> server are described in <xref linkend="multibyte-charset-supported"/>. See below for additional restrictions. 新しいデータベースで使われる文字セット符号化方式です。 文字列定数(例えば'SQL_ASCII')、整数の符号化方式番号、DEFAULTのいずれかを指定します。 DEFAULTとすると、デフォルトの符号化方式(すなわちテンプレートデータベースの符号化方式)を使います。 PostgreSQLサーバでサポートされる文字セットについては24.3.1で説明します。 この他の制限については後述します。

strategy #

Strategy to be used in creating the new database. If the <literal>WAL_LOG</literal> strategy is used, the database will be copied block by block and each block will be separately written to the write-ahead log. This is the most efficient strategy in cases where the template database is small, and therefore it is the default. The older <literal>FILE_COPY</literal> strategy is also available. This strategy writes a small record to the write-ahead log for each tablespace used by the target database. Each such record represents copying an entire directory to a new location at the filesystem level. While this does reduce the write-ahead log volume substantially, especially if the template database is large, it also forces the system to perform a checkpoint both before and after the creation of the new database. In some situations, this may have a noticeable negative impact on overall system performance. 新規データベースの作成に用いられる手法。 WAL_LOG手法が用いられる場合、データベースはブロックごとにコピーされ、各ブロックは個別に先行書き込みログに書き込まれます。 これはテンプレートデータベースが小さい場合に最も効率的な手法であるため、デフォルトです。 古いFILE_COPY手法も利用可能です。 この手法は、ターゲットデータベースが使用する各テーブル空間のための小さなレコードを先行書き込みログに書き込みます。 各レコードは、ファイルシステムレベルでディレクトリ全体を新しい場所にコピーすることを表します。 これにより先行書き込みログの量が大幅に減少しますが、特にテンプレートデータベースが大きい場合は、新規データベースの作成前後にチェックポイントを実行するようにシステムに強制もします。 状況によっては、これはシステム全体のパフォーマンスに顕著な悪影響があるかもしれません。

locale #

Sets the default collation order and character classification in the new database. Collation affects the sort order applied to strings, e.g., in queries with <literal>ORDER BY</literal>, as well as the order used in indexes on text columns. Character classification affects the categorization of characters, e.g., lower, upper, and digit. Also sets the associated aspects of the operating system environment, <literal>LC_COLLATE</literal> and <literal>LC_CTYPE</literal>. The default is the same setting as the template database. See <xref linkend="collation-managing-create-libc"/> and <xref linkend="collation-managing-create-icu"/> for details. 新しいデータベースのデフォルト照合順序および文字分類を設定します。 照合順序は、たとえば、ORDER BYを使用した問い合わせ内の文字列に適用されるソート順に影響を与えます。また、テキスト列のインデックスで使用される順序にも影響を与えます。 文字分類は、文字の分類、たとえば、小文字、大文字および数字に影響を与えます。 また、オペレーティングシステム環境の関連する側面、LC_COLLATEおよびLC_CTYPEも設定します。 デフォルトはテンプレートデータベースと同じ設定です。 詳細は、24.2.2.3.1および24.2.2.3.2を参照してください。

Can be overridden by setting <xref linkend="create-database-lc-collate"/>, <xref linkend="create-database-lc-ctype"/>, or <xref linkend="create-database-icu-locale"/> individually. lc_collatelc_ctype、またはicu_localeを個別に設定することで上書きできます。

ヒント

The other locale settings <xref linkend="guc-lc-messages"/>, <xref linkend="guc-lc-monetary"/>, <xref linkend="guc-lc-numeric"/>, and <xref linkend="guc-lc-time"/> are not fixed per database and are not set by this command. If you want to make them the default for a specific database, you can use <literal>ALTER DATABASE ... SET</literal>. その他のロケール設定lc_messageslc_monetarylc_numericlc_timeはデータベース毎には固定されず、このコマンドでは設定されません。 特定のデータベースのデフォルトにしたい場合には、ALTER DATABASE ... SETが使えます。

lc_collate #

Sets <literal>LC_COLLATE</literal> in the database server's operating system environment. The default is the setting of <xref linkend="create-database-locale"/> if specified, otherwise the same setting as the template database. See below for additional restrictions. データベースサーバのオペレーティングシステム環境でLC_COLLATEを設定します。 デフォルトは、指定されている場合はlocaleの設定、それ以外の場合はテンプレートデータベースと同じ設定です。 追加の制限については、次を参照してください。

If <xref linkend="create-database-locale-provider"/> is <literal>libc</literal>, also sets the default collation order to use in the new database, overriding the setting <xref linkend="create-database-locale"/>. locale_providerlibcの場合、新しいデータベースで使用するデフォルト照合順序も設定され、localeの設定が上書きされます。

lc_ctype #

Sets <literal>LC_CTYPE</literal> in the database server's operating system environment. The default is the setting of <xref linkend="create-database-locale"/> if specified, otherwise the same setting as the template database. See below for additional restrictions. データベースサーバのオペレーティングシステム環境でLC_CTYPEを設定します。 デフォルトは、指定されている場合はlocaleの設定で、それ以外の場合はテンプレートデータベースと同じ設定です。 追加の制限については、次を参照してください。

If <xref linkend="create-database-locale-provider"/> is <literal>libc</literal>, also sets the default character classification to use in the new database, overriding the setting <xref linkend="create-database-locale"/>. locale_providerlibcの場合は、新しいデータベースで使用するデフォルトの文字分類も設定し、localeの設定を上書きします。

icu_locale #

Specifies the ICU locale (see <xref linkend="collation-managing-create-icu"/>) for the database default collation order and character classification, overriding the setting <xref linkend="create-database-locale"/>. The <link linkend="create-database-locale-provider">locale provider</link> must be ICU. The default is the setting of <xref linkend="create-database-locale"/> if specified; otherwise the same setting as the template database. データベースのデフォルトの照合順序と文字分類に対するICUロケール(24.2.2.3.2 を参照)を指定します。 locale の設定をオーバーライドします。 ロケールプロバイダはICUでなければなりません。 デフォルトは、指定されている場合はlocaleの設定、それ以外の場合はテンプレートデータベースと同じ設定です。

icu_rules #

Specifies additional collation rules to customize the behavior of the default collation of this database. This is supported for ICU only. See <xref linkend="icu-tailoring-rules"/> for details. このデータベースのデフォルトの照合の動作をカスタマイズするための追加の照合規則を指定します。 これはICUのみでサポートされています。 詳細は24.2.3.4を参照してください。

locale_provider #

Specifies the provider to use for the default collation in this database. Possible values are <literal>icu</literal><indexterm><primary>ICU</primary></indexterm> (if the server was built with ICU support) or <literal>libc</literal>. By default, the provider is the same as that of the <xref linkend="create-database-template"/>. See <xref linkend="locale-providers"/> for details. このデータベースのデフォルトの照合順序に使用するプロバイダを指定します。 使用可能な値は、(サーバがICUをサポートするよう構築されている場合には)icuまたはlibcです。 libcがデフォルトです。 デフォルトでは、プロバイダはtemplateのプロバイダと同じです。 詳細は24.1.4を参照してください。

collation_version #

Specifies the collation version string to store with the database. Normally, this should be omitted, which will cause the version to be computed from the actual version of the database collation as provided by the operating system. This option is intended to be used by <command>pg_upgrade</command> for copying the version from an existing installation. データベースに保存する照合順序のバージョン文字列を指定します。 通常、これは省略すべきで、その場合、オペレーティングシステムが提供するデータベースの照合順序の実際のバージョンからバージョンが計算されます。 このオプションは、pg_upgradeが既存のインストールからバージョンをコピーするのに使用することを意図しています。

See also <xref linkend="sql-alterdatabase"/> for how to handle database collation version mismatches. データベースの照合順序のバージョンの不一致を処理する方法については、ALTER DATABASEも参照してください。

tablespace_name #

The name of the tablespace that will be associated with the new database, or <literal>DEFAULT</literal> to use the template database's tablespace. This tablespace will be the default tablespace used for objects created in this database. See <xref linkend="sql-createtablespace"/> for more information. 新しいデータベースに関連付けされるデフォルトのテーブル空間名です。 テンプレートデータベースのテーブル空間を使用する場合は、DEFAULTと指定します。 このテーブル空間が、このデータベースで作成されるオブジェクトのデフォルトのテーブル空間となります。 詳細はCREATE TABLESPACEを参照してください。

allowconn #

If false then no one can connect to this database. The default is true, allowing connections (except as restricted by other mechanisms, such as <literal>GRANT</literal>/<literal>REVOKE CONNECT</literal>). falseの場合、誰もこのデータベースに接続できません。 デフォルトはtrueで、接続が可能です(GRANT/REVOKE CONNECTなど他の仕掛けで制限されている場合を除きます)。

connlimit #

How many concurrent connections can be made to this database. -1 (the default) means no limit. このデータベースで確立できる同時接続数です。 -1(デフォルト)は無制限を意味します。

istemplate #

If true, then this database can be cloned by any user with <literal>CREATEDB</literal> privileges; if false (the default), then only superusers or the owner of the database can clone it. trueの場合、CREATEDB権限があれば、どのユーザでもこのデータベースを複製できます。 false(デフォルト)の場合、スーパーユーザまたはデータベースの所有者だけが複製できます。

oid #

The object identifier to be used for the new database. If this parameter is not specified, <productname>PostgreSQL</productname> will choose a suitable OID automatically. This parameter is primarily intended for internal use by <application>pg_upgrade</application>, and only <application>pg_upgrade</application> can specify a value less than 16384. 新しいデータベースに使用されるオブジェクト識別子です。 このパラメータが指定されない場合、PostgreSQLは適切なOIDを自動的に選択します。 このパラメータは主にpg_upgradeが内部的に使用することを意図しており、pg_upgradeのみが16384より小さい値を指定できます。

Optional parameters can be written in any order, not only the order illustrated above. オプションのパラメータは、任意の順番で記述できます。上記の順番で記述しなくても構いません。

注釈

<title>Notes</title>

<command>CREATE DATABASE</command> cannot be executed inside a transaction block. CREATE DATABASEはトランザクションブロックの内側では実行できません。

Errors along the line of <quote>could not initialize database directory</quote> are most likely related to insufficient permissions on the data directory, a full disk, or other file system problems. ほとんどの場合、could not initialize database directoryという行が含まれるエラーは、データディレクトリの権限不足、ディスク容量不足などファイルシステムについての問題に関連するものです。

Use <link linkend="sql-dropdatabase"><command>DROP DATABASE</command></link> to remove a database. データベースを削除するにはDROP DATABASEを使用してください。

The program <xref linkend="app-createdb"/> is a wrapper program around this command, provided for convenience. createdbプログラムは利便性のために提供される、このコマンドのラッパープログラムです。

Database-level configuration parameters (set via <link linkend="sql-alterdatabase"><command>ALTER DATABASE</command></link>) and database-level permissions (set via <link linkend="sql-grant"><command>GRANT</command></link>) are not copied from the template database. データベースレベルの設定パラメータ(ALTER DATABASEによって設定されるもの)とデータベースレベルの権限(GRANTによって設定されるもの)はテンプレートデータベースからコピーされません。

Although it is possible to copy a database other than <literal>template1</literal> by specifying its name as the template, this is not (yet) intended as a general-purpose <quote><command>COPY DATABASE</command></quote> facility. The principal limitation is that no other sessions can be connected to the template database while it is being copied. <command>CREATE DATABASE</command> will fail if any other connection exists when it starts; otherwise, new connections to the template database are locked out until <command>CREATE DATABASE</command> completes. See <xref linkend="manage-ag-templatedbs"/> for more information. template1以外のデータベースの名前をテンプレートに指定してデータベースをコピーすることは可能ですが、これは(まだ)一般的に使用するCOPY DATABASE機能として意図されているわけではありません。 主な制限は、コピー中に他のセッションからテンプレートデータベースへの接続ができないことです。 CREATE DATABASEは、開始した時に他の接続があると失敗します。 テンプレートデータベースへの新規接続はCREATE DATABASEが完了するまでできません。 詳細は23.3を参照してください。

The character set encoding specified for the new database must be compatible with the chosen locale settings (<literal>LC_COLLATE</literal> and <literal>LC_CTYPE</literal>). If the locale is <literal>C</literal> (or equivalently <literal>POSIX</literal>), 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.) <command>CREATE DATABASE</command> will allow superusers to specify <literal>SQL_ASCII</literal> 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. 新しいデータベース用に指定される文字セット符号化方式は選択されたロケール設定(LC_COLLATEおよびLC_CTYPE)と互換性がなければなりません。 ロケールがC(や同等のPOSIX)であれば、すべての符号化方式が許されますが、他のロケール設定では適切に動作する符号化方式は1つしかありません。 (しかしWindowsではUTF-8符号化方式をすべてのロケールで使用できます。) CREATE DATABASEでは、ロケール設定に関係なくスーパーユーザがSQL_ASCII符号化方式を指定することを許していますが、こうした選択は廃止予定であり、データベース内にロケールと互換性がない符号化方式でデータが格納された場合、文字列関数の誤動作を多く引き起こします。

The encoding and locale settings must match those of the template database, except when <literal>template0</literal> 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 by <literal>LC_COLLATE</literal> and <literal>LC_CTYPE</literal>. Copying such data would result in a database that is corrupt according to the new settings. <literal>template0</literal>, however, is known to not contain any data or indexes that would be affected. 符号化方式とロケール設定はテンプレートデータベースのこれらの設定と一致しなければなりません。 ただしtemplate0がテンプレートとして使用される場合は例外です。 他のデータベースには指定された符号化方式と一致しないデータを含む可能性やLC_COLLATEおよびLC_CTYPEがソート順序に影響するようなインデックスを含む可能性があることがこの理由です。 こうしたデータをコピーしたものは、新しい設定から見ると破損したデータベースとなります。 しかしtemplate0には影響を受けるデータやインデックスが含まれていないことが分かっています。

There is currently no option to use a database locale with nondeterministic comparisons (see <link linkend="sql-createcollation"><command>CREATE COLLATION</command></link> for an explanation). If this is needed, then per-column collations would need to be used. 現時点では、非決定論的な比較でデータベースのロケールを使用するオプションはありません(CREATE COLLATIONを参照してください)。 これが必要な場合は、列単位の照合順序を使用する必要があります。

The <literal>CONNECTION LIMIT</literal> option is only enforced approximately; if two new sessions start at about the same time when just one connection <quote>slot</quote> remains for the database, it is possible that both will fail. Also, the limit is not enforced against superusers or background worker processes. CONNECTION LIMITは厳密な制限ではありません。 データベース向けの接続スロットが1つ残っていた時に同時に2つの新しいセッション開始要求があった場合、両方とも失敗する可能性があります。 また、この制限はスーパーユーザおよびバックグラウンドのワーカープロセスには強制されません。

<title>Examples</title>

To create a new database: 新しいデータベースを作成します。

CREATE DATABASE lusiadas;

To create a database <literal>sales</literal> owned by user <literal>salesapp</literal> with a default tablespace of <literal>salesspace</literal>: ユーザsalesappを所有者、salesspaceをデフォルトのテーブル空間としてデータベースsalesを作成します。

CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;

To create a database <literal>music</literal> with a different locale: 別のロケールでデータベースmusicを作成します。

CREATE DATABASE music
    LOCALE 'sv_SE.utf8'
    TEMPLATE template0;

In this example, the <literal>TEMPLATE template0</literal> clause is required if the specified locale is different from the one in <literal>template1</literal>. (If it is not, then specifying the locale explicitly is redundant.) この例において、指定するロケールがtemplate1のロケールと異なる場合、TEMPLATE template0の句が必須となります。 (それらが同じ場合、ロケールを明示的に指定することは必要ありません。)

To create a database <literal>music2</literal> with a different locale and a different character set encoding: 別のロケールおよび別の文字セット符号化方式でデータベースmusic2を作成します。

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. 指定するロケールと符号化方式の設定は対応するものでなければならず、そうでなければエラーが報告されます。

Note that locale names are specific to the operating system, so that the above commands might not work in the same way everywhere. ロケール名はOSに固有のものであるため、上記のコマンドはすべての環境で同じように動作するとは限らないことに注意してください。

互換性

<title>Compatibility</title>

There is no <command>CREATE DATABASE</command> statement in the SQL standard. Databases are equivalent to catalogs, whose creation is implementation-defined. 標準SQLにはCREATE DATABASE文はありません。 データベースはカタログに相当するもので、その作成は実装依存です。

関連項目

<title>See Also</title> ALTER DATABASE, DROP DATABASE