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

23.3. テンプレートデータベース #

<title>Template Databases</title>

<command>CREATE DATABASE</command> actually works by copying an existing database. By default, it copies the standard system database named <literal>template1</literal>.<indexterm><primary>template1</primary></indexterm> Thus that database is the <quote>template</quote> from which new databases are made. If you add objects to <literal>template1</literal>, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language <application>PL/Perl</application> in <literal>template1</literal>, it will automatically be available in user databases without any extra action being taken when those databases are created. 実際のCREATE DATABASEの動作は、既存のデータベースをコピーすることです。 デフォルトでは、template1という名前の標準のシステムデータベースをコピーします。 したがって、このデータベースは新しく作成するデータベースの元になるテンプレートとなります。 template1にオブジェクトを追加した場合、追加したオブジェクトはその後に作成されるユーザデータベースにコピーされます。 この振舞いによって、データベース標準オブジェクト群にサイト独自の変更を行うことができます。 例えば、PL/Perl手続き言語をtemplate1にインストールした場合、データベースを作成する時に追加作業を行うことなく、自動的にこの言語をユーザデータベースで使用できます。

There is a second standard system database named <literal>template0</literal>.<indexterm><primary>template0</primary></indexterm> This database contains the same data as the initial contents of <literal>template1</literal>, that is, only the standard objects predefined by your version of <productname>PostgreSQL</productname>. <literal>template0</literal> should never be changed after the database cluster has been initialized. By instructing <command>CREATE DATABASE</command> to copy <literal>template0</literal> instead of <literal>template1</literal>, you can create a <quote>pristine</quote> user database (one where no user-defined objects exist and where the system objects have not been altered) that contains none of the site-local additions in <literal>template1</literal>. This is particularly handy when restoring a <literal>pg_dump</literal> dump: the dump script should be restored in a pristine database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to <literal>template1</literal> later on. template0という名前の二次的な標準システムデータベースがあります。 このデータベースにはtemplate1の初期内容と同じデータが含まれています。 つまり、使用しているバージョンのPostgreSQLで定義済みの標準オブジェクトのみから構成されています。 template0をデータベースクラスタを初期化した後に変更してはいけません。 CREATE DATABASEtemplate1ではなくtemplate0をコピーするように実行することで、template1に追加されたサイト独自のものを含まない、(そこではユーザ定義オブジェクトは存在せず、システムオブジェクトは変更されていない)汚れがないユーザデータベースを作成できます。 これは特に、pg_dumpダンプからリストアする時に便利です。 このダンプスクリプトは、後でtemplate1に追加される可能性のあるオブジェクトと衝突しないように、ダンプしたデータベースの内容を正しく再作成するために、汚れのないデータベースにリストアされなければなりません。

Another common reason for copying <literal>template0</literal> instead of <literal>template1</literal> is that new encoding and locale settings can be specified when copying <literal>template0</literal>, whereas a copy of <literal>template1</literal> must use the same settings it does. This is because <literal>template1</literal> might contain encoding-specific or locale-specific data, while <literal>template0</literal> is known not to. template1の代わりにtemplate0をコピーするその他の一般的な理由は、template0をコピーするときに新規の符号化方式とロケールを設定できることです。 一方、template1のコピーはそれが行ったと同一の設定を使用しなければなりません。 これはtemplate0が認識されていない一方で、template1が符号化方式特有の、またはロケール特有のデータを含んでいる可能性があることに依ります。

To create a database by copying <literal>template0</literal>, use: template0をコピーしてデータベースを作成するには、

CREATE DATABASE dbname TEMPLATE template0;

from the SQL environment, or: をSQL環境から実行するか、または

createdb -T template0 dbname

from the shell. をシェルから実行します。

It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for <command>CREATE DATABASE</command>. It is important to understand, however, that 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 source database while it is being copied. <command>CREATE DATABASE</command> will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented. さらにテンプレートデータベースを作成することができます。 また、実際のところCREATE DATABASEのテンプレートとして名前を指定することで、クラスタ内の任意のデータベースをコピーできます。 しかし、この機能は、(まだ)汎用目的のCOPY DATABASE能力を意図したものではないことを理解しておいてください。 コピー操作の間、他のセッションから元のデータベースに接続することができないという点は大きな制限です。 CREATE DATABASEは、その起動時に他の接続が存在する場合失敗します。 コピー操作中は元のデータベースへの新しい接続を許しません。

Two useful flags exist in <literal>pg_database</literal><indexterm><primary>pg_database</primary></indexterm> for each database: the columns <literal>datistemplate</literal> and <literal>datallowconn</literal>. <literal>datistemplate</literal> can be set to indicate that a database is intended as a template for <command>CREATE DATABASE</command>. If this flag is set, the database can be cloned by any user with <literal>CREATEDB</literal> privileges; if it is not set, only superusers and the owner of the database can clone it. If <literal>datallowconn</literal> is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). The <literal>template0</literal> database is normally marked <literal>datallowconn = false</literal> to prevent its modification. Both <literal>template0</literal> and <literal>template1</literal> should always be marked with <literal>datistemplate = true</literal>. datistemplate列とdatallowconn列という、データベースそれぞれに有用なフラグがpg_databaseに存在します。 datistemplateは、そのデータベースがCREATE DATABASEのテンプレートとして使用されることを目的としているものであることを意味するために設定できます。 このフラグが設定された場合、CREATEDB権限を持つすべてのユーザはそのデータベースを複製できます。 設定されていない場合は、スーパーユーザとそのデータベース所有者のみがそれを複製できます。 datallowconnが偽の場合、そのデータベースへの新規接続はできません(しかし、このフラグを偽にするだけでは既存のセッションは閉ざされません)。 template0データベースは、変更を防ぐために、通常datallowconn = falseとされています。 template0template1の両方は、常にdatistemplate = trueとされていなければなりません。

注記

<literal>template1</literal> and <literal>template0</literal> do not have any special status beyond the fact that the name <literal>template1</literal> is the default source database name for <command>CREATE DATABASE</command>. For example, one could drop <literal>template1</literal> and recreate it from <literal>template0</literal> without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in <literal>template1</literal>. (To delete <literal>template1</literal>, it must have <literal>pg_database.datistemplate = false</literal>.) template1template0には、template1という名前がCREATE DATABASEのデフォルトのソースデータベースの名前であること以上の特別な地位はありません。 例えば、template1を削除し、template0から再作成しても何も問題ありません。 この操作は、不注意にごみをtemplate1に追加してしまった場合にお勧めします。 (template1を削除するには、pg_database.datistemplate = falseとしなければなりません。)

The <literal>postgres</literal> database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of <literal>template1</literal> and can be dropped and recreated if necessary. データベースクラスタが初期化される時、postgresデータベースも作成されます。 このデータベースは、ユーザとアプリケーションのデフォルトの接続先を意図したものです。 これはtemplate1の単純なコピーで、必要に応じて削除したり再作成したりできます。