ひとたびデータベースが作成されると以下の方法で接続することが出来ます。
SQL 命令を対話的に入力、編集および実行する Postgres のターミナルモニタプログラム (psql) を利用する。
writing a C program using the LIBPQ subroutine library. This allows you to submit SQL commands from C and get answers and status messages back to your program. This interface is discussed further in section ??.
サブルーチンインターフェイス libpq を 使った C プログラムを書く。この場合 SQL 命令 を C から発行し、結果とステータスメッセージをプログラムが受け取り ます。このインターフェイスは ?? 章で更に検討されます。 (訳注:?? は manage-ag.sgml では PostgreSQL Programmer's Guide となっています。)
% psql mydbYou 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=> \hOnce you have finished entering your queries into the workspace, you can pass the contents of the workspace to the Postgres server by typing:
mydb=> \gThis 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 fileNameTo get out of psql and return to UNIX, type
mydb=> \qand 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 fileNamepsql から抜けて UNIX に戻るには、
mydb=> \qと入力します。ここで psql は終了し コマンドシェルに戻ります。 (他のエスケープコードについては、モニタのプロンプトで \h を入力して下さい。) 空白(すなわち、スペース、タブ、改行)は SQL 文の中で 自由に使うことができます。一行のコメントは二つのダッシュ "--" で表します。ダッシュの後の行の最後まではすべて無視 されます。 複数行のコメントと、行の中にいれるコメントは "/* ... */" で表します。
TBD