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

23.2. データベースの作成 #

<title>Creating a Database</title>

In order to create a database, the <productname>PostgreSQL</productname> server must be up and running (see <xref linkend="server-start"/>). データベースを作成する場合、PostgreSQLサーバを起動している必要があります(19.3を参照してください)。

Databases are created with the SQL command <xref linkend="sql-createdatabase"/>: CREATE DATABASESQLコマンドでデータベースを作成できます。

CREATE DATABASE name;

where <replaceable>name</replaceable> follows the usual rules for <acronym>SQL</acronym> identifiers. The current role automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later (which also removes all the objects in it, even if they have a different owner). ここで、nameSQL識別子の通常の規則に従います。 現在のロールが自動的に新しいデータベースの所有者になります。 作成後、データベースを削除する権限はこの所有者にあります(この作業では、そのデータベースに属している、所有者のものではないオブジェクトでも、すべて削除されます)。

The creation of databases is a restricted operation. See <xref linkend="role-attributes"/> for how to grant permission. データベースの作成は制限された作業です。 権限の付与に関する詳細は22.2を参照してください。

Since you need to be connected to the database server in order to execute the <command>CREATE DATABASE</command> command, the question remains how the <emphasis>first</emphasis> database at any given site can be created. The first database is always created by the <command>initdb</command> command when the data storage area is initialized. (See <xref linkend="creating-cluster"/>.) This database is called <literal>postgres</literal>.<indexterm><primary>postgres</primary></indexterm> So to create the first <quote>ordinary</quote> database you can connect to <literal>postgres</literal>. CREATE DATABASEコマンドを実行するためには、データベースサーバに接続している必要があります。 そうすると、あるサイトの最初のデータベースはどのようにして作成するのかという疑問が出てきます。 最初のデータベースはinitdbコマンドでデータ格納領域が初期化される時、必ず作成されます。 (19.2を参照してください。) このデータベースはpostgresと呼ばれます。 したがって、最初の通常のデータベースを作成するにはpostgresに接続してください。

Two additional databases, <literal>template1</literal><indexterm><primary>template1</primary></indexterm> and <literal>template0</literal>,<indexterm><primary>template0</primary></indexterm> are also created during database cluster initialization. Whenever a new database is created within the cluster, <literal>template1</literal> is essentially cloned. This means that any changes you make in <literal>template1</literal> are propagated to all subsequently created databases. Because of this, avoid creating objects in <literal>template1</literal> unless you want them propagated to every newly created database. <literal>template0</literal> is meant as a pristine copy of the original contents of <literal>template1</literal>. It can be cloned instead of <literal>template1</literal> when it is important to make a database without any such site-local additions. More details appear in <xref linkend="manage-ag-templatedbs"/>. template1template0という2つの追加のデータベースもデータベースクラスタの初期化時に作成されます。 クラスタ内に新しいデータベースが作成されたら、実際にtemplate1が複製されます。 つまりtemplate1に変更を与えると、その後に作成されるデータベースすべてにその変更が反映されることを意味します。 このため新しく作成するデータベースすべてに反映させたい場合でない限りtemplate1内にオブジェクトを作成することは避けてください。 template0template1の元の内容の汚れのない複製を意図したものです。 サイト独自の追加などを一切含まないデータベースを作成することが重要な場合に、template1の代わりに複製できます。 詳細については23.3を参照してください。

As a convenience, there is a program you can execute from the shell to create new databases, <command>createdb</command>.<indexterm><primary>createdb</primary></indexterm> 利便性のために、シェルからcreatedbを実行して、新しいデータベースを作成できます。

createdb dbname

<command>createdb</command> does no magic. It connects to the <literal>postgres</literal> database and issues the <command>CREATE DATABASE</command> command, exactly as described above. The <xref linkend="app-createdb"/> reference page contains the invocation details. Note that <command>createdb</command> without any arguments will create a database with the current user name. createdbは魔法ではありません。 これはpostgresデータベースに接続し、先に解説した通りにCREATE DATABASEコマンドを実行します。 createdbのマニュアルページに実行方法の詳細が説明されています。 引数のないcreatedbは現在のユーザ名のデータベースを作成しますので、注意してください。

注記

<xref linkend="client-authentication"/> contains information about how to restrict who can connect to a given database. 特定のデータベースに誰が接続できるかを制限する方法については第21章に記載されています。

Sometimes you want to create a database for someone else, and have them become the owner of the new database, so they can configure and manage it themselves. To achieve that, use one of the following commands: 他のユーザのためにデータベースを作成し、そのユーザ自身が設定、管理できるように新しいデータベースの所有者にさせたい場合も考えられます。 そのためには、次のコマンドのいずれかを使用します。 SQL環境からは

CREATE DATABASE dbname OWNER rolename;

from the SQL environment, or: シェルからは

createdb -O rolename dbname

from the shell. Only the superuser is allowed to create a database for someone else (that is, for a role you are not a member of). 他のユーザのために(つまり、自身がメンバではないロールのために)データベースを作成できるのはスーパーユーザだけです。