Postgres を使ってみる

After Postgres is installed, a database system is created, a postmaster daemon is running, and the regression tests have passed, you'll want to see Postgres do something. That's easy. Invoke the interactive interface to Postgres, psql:

無事 Postgres のインストールも終了、データベースシステムの作成も行い、postmaster デーモンも起動して、regression テストも順調にパスしました。 Postgres が動作していている事を確認したいと思っているでしょうが、それは非常に簡単です。psql という Postgres の対話型インターフェースを使用してみましょう:

% psql template1
(psql has to open a particular database, but at this point the only one that exists is the template1 database, which always exists. We will connect to it only long enough to create another one and switch to it.) (psql は起動時に必ず何かデータベースを開かなくてはなりませんが、現在は initdb実行時に作成される template1 というデータベースしか存在していません。 新しいデータベースを作るまでは、とりあえずそれに接続しておきます。)

The response from psql is:

以下のように表示されれば、データベースに問題無く接続されています:

Welcome to the POSTGRESQL interactive sql monitor:
  Please read the file COPYRIGHT for copyright terms of POSTGRESQL

   type \? for help on slash commands
   type \q to quit
   type \g or terminate with semicolon to execute query
 You are currently connected to the database: template1

template1=>

Create the database foo:

foo というデータベースを作ってみます:

template1=> create database foo;
CREATEDB
(Get in the habit of including those SQL semicolons. Psql won't execute anything until it sees the semicolon or a "\g" and the semicolon is required to delimit multiple statements.) (SQL では文の最後に必ずセミコロンが付きます。 psql は、セミコロンまた"\g" が入力されるまで何も実行しません。 このセミコロンは複数行に渡るSQL文の終わりを限定するのに必要です。)

Now connect to the new database:

新しく作ったデータベースに接続してみます:

template1=> \c foo
connecting to new database: foo
("slash" commands aren't SQL, so no semicolon. Use \? to see all the slash commands.) ("バックスラッシュ"コマンドは、SQL ではないので、文末のセミコロンは必要ありません。 \? と入力すれば、バックスラッシュコマンドの一覧が表示されます。)

And create a table:

テーブルを作ります:

foo=> create table bar (i int4, c char(16));
CREATE

Then inspect the new table:

新しく作ったテーブルを確認してみます:

foo=> \d bar

Table    = bar
+----------------------------------+----------------------------------+-------+
|              Field               |              Type                | Length|
+----------------------------------+----------------------------------+-------+
| i                                | int4                             |     4 |
| c                                | (bp)char                         |    16 |
+----------------------------------+----------------------------------+-------+

And so on. You get the idea.

どのように新しいデータベースやテーブルを作ったら良いか分かったかと思います。