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

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

<title>Creating a Database</title>

The first test to see whether you can access the database server is to try to create a database. A running <productname>PostgreSQL</productname> server can manage many databases. Typically, a separate database is used for each project or for each user. データベースサーバにアクセスできるかどうかがわかる最初の試験は、データベースの作成を試みることです。 稼働中のPostgreSQLサーバは多くのデータベースを管理できます。 典型的には、プロジェクトやユーザごとに別々のデータベースが使用されます。

Possibly, your site administrator has already created a database for your use. In that case you can omit this step and skip ahead to the next section. サイト管理者により使用できるデータベースが既に作成されている場合もあります。 この場合、この段階を飛ばして、次の節まで進んでください。

To create a new database, in this example named <literal>mydb</literal>, you use the following command: 新しいデータベースを作成するには、以下のコマンドを使用してください。 この例ではmydbという名前です。

$ createdb mydb

If this produces no response then this step was successful and you can skip over the remainder of this section. この手順で何も応答がなければ、この段階は成功し、本節の残りは飛ばすことができます。

If you see a message similar to: 以下のようなメッセージが現れた場合、PostgreSQLが正しくインストールされていません。

createdb: command not found

then <productname>PostgreSQL</productname> was not installed properly. Either it was not installed at all or your shell's search path was not set to include it. Try calling the command with an absolute path instead: PostgreSQLがインストールされていないか、シェルの検索パスに含まれていないのいずれかです。 代わりに絶対パスでそのコマンドを実行してみてください。

$ /usr/local/pgsql/bin/createdb mydb

The path at your site might be different. Contact your site administrator or check the installation instructions to correct the situation. このパスはサイトによって異なるかもしれません。 この問題を解決するには、サイト管理者に連絡するか、インストール取扱説明書を調べてください。

Another response could be this: 他の応答として以下もあります。

createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?

This means that the server was not started, or it is not listening where <command>createdb</command> expects to contact it. Again, check the installation instructions or consult the administrator. これは、サーバが起動していないか、createdbが想定している状態でサーバがリッスンしていないかを示しています。 こちらも、インストール手順を点検するか管理者に相談してください。

Another response could be this: 他の応答として以下もあります。

createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  role "joe" does not exist

where your own login name is mentioned. This will happen if the administrator has not created a <productname>PostgreSQL</productname> user account for you. (<productname>PostgreSQL</productname> user accounts are distinct from operating system user accounts.) If you are the administrator, see <xref linkend="user-manag"/> for help creating accounts. You will need to become the operating system user under which <productname>PostgreSQL</productname> was installed (usually <literal>postgres</literal>) to create the first user account. It could also be that you were assigned a <productname>PostgreSQL</productname> user name that is different from your operating system user name; in that case you need to use the <option>-U</option> switch or set the <envar>PGUSER</envar> environment variable to specify your <productname>PostgreSQL</productname> user name. ここでjoeのところには、ログインした時のユーザ名が記載されています。 これは、管理者がそのユーザ用のPostgreSQLユーザアカウントを作成していない時に起こります (PostgreSQLユーザアカウントは、オペレーティングシステムのユーザアカウントとは別です)。 自身が管理者なら、アカウントの作成方法に関して第22章を参照してください。 最初のユーザアカウントを作成するためには、PostgreSQLをインストールした時のオペレーティングシステムのユーザ(通常postgres)になる必要があります。 PostgreSQLユーザアカウントがオペレーティングシステムのユーザ名と異なる名前で用意されているかもしれません。 その場合は、PostgreSQLのユーザ名を指定するために、-Uスイッチを使用するか、PGUSER環境変数を設定する必要があります。

If you have a user account but it does not have the privileges required to create a database, you will see the following: ユーザアカウントを持っているが、データベースを作成するために必要な権限を持っていない場合は、以下のメッセージが現れます。

createdb: error: database creation failed: ERROR:  permission denied to create database

Not every user has authorization to create new databases. If <productname>PostgreSQL</productname> refuses to create databases for you then the site administrator needs to grant you permission to create databases. Consult your site administrator if this occurs. If you installed <productname>PostgreSQL</productname> yourself then you should log in for the purposes of this tutorial under the user account that you started the server as. 全てのユーザがデータベースを新規に作成できる権限を持っているわけではありません。 PostgreSQLがデータベースの作成を拒否した場合、サイト管理者からデータベースを作成する権限を付与してもらう必要があります。 これが発生した場合はサイト管理者に相談してください。 自身でPostgreSQLをインストールしたのであれば、このチュートリアルを実行する時は、サーバを起動したユーザアカウントでログインしてください。 [1]

You can also create databases with other names. <productname>PostgreSQL</productname> allows you to create any number of databases at a given site. Database names must have an alphabetic first character and are limited to 63 bytes in length. A convenient choice is to create a database with the same name as your current user name. Many tools assume that database name as the default, so it can save you some typing. To create that database, simply type: 他の名前のデータベースを作成することもできます。 PostgreSQLは、与えられたサイトでいくつでもデータベースを作成することを許可しています。 データベース名は、先頭がアルファベット文字から始まる、63バイトまでの長さでなければなりません。 簡単な選択は、現在のユーザ名と同じ名前のデータベースを作成することです。 多くのツールでは、データベース名のデフォルトとしてそれを仮定していますので、これにより入力数を減らすことができます。 このデータベースを作成するには、単純に以下を実行します。

$ createdb

If you do not want to use your database anymore you can remove it. For example, if you are the owner (creator) of the database <literal>mydb</literal>, you can destroy it using the following command: データベースを使用しなくなったら、削除できます。 例えば、mydbデータベースの所有者(作成者)であれば、以下のコマンドでそれを廃棄できます。

$ dropdb mydb

(For this command, the database name does not default to the user account name. You always need to specify it.) This action physically removes all files associated with the database and cannot be undone, so this should only be done with a great deal of forethought. (このコマンドでは、データベース名のデフォルトはユーザアカウント名ではありません。 常に指定しなければなりません。) この動作は、そのデータベースに関する全てのファイルを物理的に削除しますので、取り消すことはできません。 事前に熟考した場合にのみこれを実施してください。

More about <command>createdb</command> and <command>dropdb</command> can be found in <xref linkend="app-createdb"/> and <xref linkend="app-dropdb"/> respectively. createdbdropdbの詳細は、それぞれcreatedbdropdbにあります。



[1] As an explanation for why this works: <productname>PostgreSQL</productname> user names are separate from operating system user accounts. When you connect to a database, you can choose what <productname>PostgreSQL</productname> user name to connect as; if you don't, it will default to the same name as your current operating system account. As it happens, there will always be a <productname>PostgreSQL</productname> user account that has the same name as the operating system user that started the server, and it also happens that that user always has permission to create databases. Instead of logging in as that user you can also specify the <option>-U</option> option everywhere to select a <productname>PostgreSQL</productname> user name to connect as. 何故これで上手くいくのかについての説明:PostgreSQLのユーザ名はオペレーティングシステムのユーザアカウントとは分離されています。 データベースに接続するとき、接続に利用するPostgreSQLのユーザ名を選択します。 選択しなければ、デフォルトで現在のオペレーティングシステムのアカウントと同じ名前となります。 これにより、サーバを開始したオペレーティングシステムのユーザと同じ名前のPostgreSQLユーザアカウントが、常に存在するようになっています。 そしてまた、このユーザは常にデータベースを作成する権限を持ちます。 そのユーザとしてログインする代わりに、-Uオプションを毎回使用して、接続するPostgreSQLのユーザ名を選択できます。