ひとたびデータベースが作成されると以下の方法で接続することが出来ます。
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 the PostgreSQL Programmer's Guide.
サブルーチンインターフェイス libpq を 使った C プログラムを書く。この場合 SQL 命令 を C から発行し、結果とステータスメッセージをプログラムが受け取り ます。このインターフェイスは PostgreSQL Programmer's Guide で更に検討されます。
% psql dbnameYou will be greeted with the following message:
Welcome to the Postgres interactive sql monitor: 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: dbname dbname=>このマニュアルではいくつかの例の検証として psql を起動してみます。 データベース dbname の 起動には、
% psql dbnameと入力します。 すると次のように表示されます。
Welcome to the Postgres interactive sql monitor: 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: dbname dbname=>
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:
dbname=> \hOnce you have finished entering your queries into the workspace, you can pass the contents of the workspace to the Postgres server by typing:
dbname=> \gThis tells the server to process the query. If you terminate your query with a semicolon, the backslash-g is not necessary. psql will automatically process semicolon terminated queries. To read queries from a file, instead of entering them interactively, type:
dbname=> \i filenameTo get out of psql and return to UNIX, type
dbname=> \qand psql will quit and return you to your command shell. (For more escape codes, type backslash-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 two dashes ("--"). Everything after the dashes up to the end of the line is ignored. Multiple-line comments, and comments within a line, are denoted by "/* ... */", a convention borrowed from Ingres.
このプロンプトはターミナルモニタが入力を待っていて、 ターミナルモニタが維持している作業スペースに SQL 文を入力出来ることを示しています。 psql プログラムはバックスラッシュ文字、"\" で始まるエスケープコードに応答します。 例えば Postgres SQL命令の 文法のヘルプが、次のように入力することで得られます:
dbname=> \h作業スペースへの問合せの入力が終了したら、作業スペースの内容を Postgres サーバへ次のように入力して 渡すことが出来ます:
dbname=> \gこれはサーバに問合せの処理を督促します。問合せをセミコロンで終ら せれば、\g は必要ありません。 psql はセミコロンで終った問合せを自動的 に処理します。対話的に入力するのではなくファイルから問合せを読み込 ませたい場合は下記を入力します:
dbname=> \i filenamepsql から抜けて UNIX に戻るには、
dbname=> \qと入力します。ここで psql は終了し コマンドシェルに戻ります。 (他のエスケープコードについては、モニタのプロンプトで \h を入力 して下さい。) 空白(すなわち、スペース、タブ、改行)は SQL 文の中で 自由に使うことができます。一行のコメントは二つのダッシュ( "--") で表します。ダッシュの後の行の最後まではすべて無視されます。 複数行のコメントと、行の中にいれるコメントは Ingres から借り受けた規約 "/* ... */" で表します。