PostgreSQLの初期化  PostgreSQLをインストールしたら、最初に一度だけ以下のコマンドで初期化を行う必要があります(パッケージからインストールした場合も同様)。 $ initdb 初期化を行ったら、データベースを作成してみましょう。ただし、この処理を行う前にpostmasterを起動しておく必要があります。次のようにして、デーモンモードで起動しておきましょう。 $ postmaster -S -iオプション“-S”はpostmasterをデーモンモードで起動することを意味します。 オプション“-i”はインターネットソケットを使用することを意味します。 コマンドラインからデータベースを作成  データベース作成コマンドは以下のとおりです。 $ createdb [データベース名] ここから先は、PostgreSQLを操作する専用のコマンドラインツール「psql」を使います。psqlを使うと、データベースの内容を参照したり、SQL文を実行したりすることができます。  では、実際にpsqlを起動してみましょう。 $ psql [データベース名]  データベース名は、ユーザー名と同一名称を付けた場合、psqlでは省略してオープンすることが可能です。psqlを起動すると、次のようなメッセージが表示されるはずです。 Welcome to the POSTGRESQL interactive sql monitor: Please read the file COPYRIGHT for copyright terms of POSTGRESQL [PostgreSQL 6.5.3 on i686-pc-linux-gnu, compiled by gcc 2.95.2] 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: postgres postgres=> postgres=> の部分がpsqlのプロンプトとなります。 データベースオブジェクトを操作する  psqlのコマンドラインから操作して、どんなデータベースオブジェクトがあるか表示してみましょう。 postgres=> \d Couldn't find any tables, sequences or indices! 上記のメッセージが出た場合、データベース内にオブジェクトは存在しません。データベース作成直後なので当然のことです。では、テーブルを作成してみましょう。 postgres=> create table test ( shainno int,shimei text); CREATE テーブルtestが作成されました。もし、intなどのスペルを間違った場合などは以下のメッセージが表示されて、テーブル作成に失敗してしまいます。 postgres=> create table test ( shainno number,shimei text); ERROR: Unable to locate type name 'number' in catalog エラーが出なければ、テーブルが作成されたかどうか確認してみましょう。 postgres=> \d Database = postgres +------------------+----------------------------------+----------+ | Owner | Relation | Type | +------------------+----------------------------------+----------+ | postgres | test | table | +------------------+----------------------------------+----------+ 上記のような表示がされれば、テーブルtestが作成されたことが確認できます。  テーブルのカラム定義を確認する場合、“\d [テーブル名]”のようにコマンドを入力します。 postgres=> \d test Table = test +--------------------------+--------------------------+-------+ | Field | Type | Length| +--------------------------+--------------------------+-------+ | shainno | int4 | 4 | | shimei | text | var | +--------------------------+--------------------------+-------+テーブルにデータを追加  では、作成したテーブルに値を入力して表示させることにしましょう。  前回も書いたとおり、PostgreSQLではSQL92のサブセットに準拠したSQLが使用できます。データの追加を行うコマンドは“insert”です。 postgres=> insert into test (shainno,shimei) values (1,'munetika'); INSERT 18506 1 追加に成功した場合、上記のようなメッセージが表示されます。失敗した場合は、以下のようなメッセージとなります。 postgres=> insert into test (shainno,shimei) values (1,munetika); ERROR: Attribute munetika not foundselect文で検索  追加したデータの表示は、“select”コマンドを使用します。 postgres=> select * from test; shainno|shimei -------+-------- 1|munetika (1 row)表示に失敗した場合、以下のようなメッセージが表示されます。 postgres=> select * from tests; ERROR: tests: Table does not exist. 最後にpsqlを終了しましょう。終了のコマンドは“\q”です。 postgres=> \q [postgres@micky pgsql]$ 上記のようにOSのプロンプトが表示され、psqlは終了します。 psqlで使えるコマンド一覧  psqlで使えるコマンドを確認したい場合、“\?”コマンドを使用します。ここでは、そのとき表示される内容を示しておきます。 postgres=> \? \? -- help \a -- toggle field-alignment (currently on) \C [] -- set html3 caption (currently '') \connect -- connect to new database (currently 'postgres') \copy table {from | to} \d [] -- list tables and indices, columns in
, or * for all \da -- list aggregates \dd []- list comment for table, field, type, function, or operator. \df -- list functions \di -- list only indices \do -- list operators \ds -- list only sequences \dS -- list system tables and indexes \dt -- list only tables \dT -- list types \e [] -- edit the current query buffer or \E [] -- edit the current query buffer or , and execute \f [] -- change field separater (currently '|') \g [] [|] -- send query to backend [and results in or pipe] \h [] -- help on syntax of sql commands, * for all commands \H -- toggle html3 output (currently off) \i -- read and execute queries from filename \l -- list all databases \m -- toggle monitor-like table display (currently off) \o [] [|] -- send all query results to stdout, , or pipe \p -- print the current query buffer \q -- quit \r -- reset(clear) the query buffer \s [] -- print history or save it in \t -- toggle table headings and row count (currently on) \T [] -- set html3.0
options (currently '') \x -- toggle expanded output (currently off) \w -- output current buffer to a file \z -- list current grant/revoke permissions \! [] -- shell escape or command