データベースへの接続

ひとたびデータベースが作成されると以下の方法で接続することが出来ます。

You might want to start up psql, to try out the examples in this manual. It can be activated for the mydb database by typing the command:
% psql mydb
You will be greeted with the following message:
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

mydb=>
このマニュアルではいくつかの例の検証として psql を起動してみます。 データベース mydb の 起動には、
% psql mydb
と入力します。 すると次のように表示されます。
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

mydb=>

This prompt indicates that the terminal monitor is listening to you and that you can type SQL queries into a workspace maintained by the terminal monitor. The psql program responds to escape codes that begin with the backslash character, "\" For example, you can get help on the syntax of various Postgres SQL commands by typing:

mydb=> \h
Once you have finished entering your queries into the workspace, you can pass the contents of the workspace to the Postgres server by typing:
mydb=> \g
This tells the server to process the query. If you terminate your query with a semicolon, the "\g" is not necessary. psql will automatically process semicolon terminated queries. To read queries from a file, say myFile, instead of entering them interactively, type:
mydb=> \i fileName
To get out of psql and return to UNIX, type
mydb=> \q
and psql will quit and return you to your command shell. (For more escape codes, type \h at the monitor prompt.) White space (i.e., spaces, tabs and newlines) may be used freely in SQL queries. Single-line comments are denoted by "--". Everything after the dashes up to the end of the line is ignored. Multiple-line comments, and comments within a line, are denoted by "/* ... */"

このプロンプトはターミナルモニタが入力を待っていて、ターミナルモニタが 維持している作業スペースに SQL 文を入力出来ることを 示しています。 psql プログラムはバックスラッシュ文字、"\" で始まるエスケープコードに応答します。 例えば Postgres SQL命令の 文法のヘルプが、次のように入力することで得られます:

mydb => \h
作業スペースへの問合せの入力が終了したら、作業スペースの内容を Postgres サーバへ次のように入力して 渡すことが出来ます:
mydb => \g
これはサーバに問合せの処理を督促します。問合せをセミコロンで終ら せれば、"\g" は必要ありません。 psql はセミコロンで終った問合せを自動的 に処理します。対話的に入力するのではなくたとえば myFile と言った ファイルから問合せを読み込ませたい場合は下記を入力します:
mydb=> \i fileName
psql から抜けて UNIX に戻るには、
mydb=> \q
と入力します。ここで psql は終了し コマンドシェルに戻ります。 (他のエスケープコードについては、モニタのプロンプトで \h を入力して下さい。) 空白(すなわち、スペース、タブ、改行)は SQL 文の中で 自由に使うことができます。一行のコメントは二つのダッシュ "--" で表します。ダッシュの後の行の最後まではすべて無視 されます。 複数行のコメントと、行の中にいれるコメントは "/* ... */" で表します。

データベースの権限

テーブルの権限

TBD