psql

Name

psql  --  Postgres interactive client Postgres インタラクティブクライアント

Synopsis

psql [ dbname ]
psql -A [ -c query ] [ -d dbname ]
    -e -E [ -f filename ] [ -F separator ]
    [ -h hostname ] -Hln [ -o filename ]
    [ -p port ] -qsSt [ -T table_o ] -ux
    [ dbname ]
  

入力

psql accepts many command-line arguments, a rich set of meta-commands, and the full SQL language supported by Postgres. The most common command-line arguments are:

psql は、 数多くのコマンドライン引数、豊富なメタコマンドのセットそして Postgres がサポートする全ての SQL 言語を受けつけます。最も一般的なコマンドライン 引数には以下のようなものがあげれれます。

dbname

The name of an existing database to access. dbname defaults to the value of the USER environment variable or, if that's not set, to the Unix account name of the current user.

アクセスする既存のデータベース名です。 デフォルトの dbnameUSER の環境変数の値となります。環境変数が設定されていない場合、 現在のユーザの Unix のアカウント名となります。

-c query

A single query to run. psql will exit on completion.

実行する単独の問合せです。 完了時点で psql は終了します。

The full set of command-line arguments and meta-commands are described in a subsequent section.

全てのコマンドライン引数とメタコマンドのセットは引き続く節で 説明されます。

There are some environment variables which can be used in liu of command line arguments. Additionally, the Postgres frontend library used by the psql application looks for other optional environment variables to configure, for example, the style of date/time representation and the local time zone. Refer to the chapter on libpq in the Programmer's Guide for more details.

いくつかの環境変数をコマンドライン引数の代わりに使用する ことが出来ます。 更に、psql で使われる Postgres フロントエンドライブラリは、 例えば日付/時間の表現スタイルやローカルなタイムゾーンなどの、 その設定用にオプションである環境変数を見に行きます。より詳細については プログラマガイドlibpq 章を参照して下さい。

You may set any of the following environment variables to avoid specifying command-line options:

特定のコマンドラインオプションを省くため以下のどんな環境 変数をセットしてもかまいません。

PGHOST

The DNS host name of the database server. Setting PGHOST to a non-zero-length string causes TCP/IP communication to be used, rather than the default local Unix domain sockets.

データベースサーバの DNS ホスト名です。 PGHOST 名が何らかの文字列で指定されると、 デフォルトのローカル Unix ドメインソケットではなく、TCP/IP 通信が使用されます。

PGPORT

The port number on which a Postgres server is listening. Defaults to 5432.

Postgres サーバが監視している ポート番号で、デフォルトは 5432 です。

PGTTY

The target for display of messages from the client support library. Not required.

クライアントのサポートライブラリからメッセージを 表示するターゲットです。必要ありません。

PGOPTION

If PGOPTION is specified, then the options it contains are parsed before any command-line options.

PGOPTION が指定されると、それが含んでいる オプションは、どんなコマンドラインオプションより 前にパースされます。

PGREALM

PGREALM only applies if Kerberos authentication is in use. If this environment variable is set, Postgres will attempt authentication with servers for this realm and will use separate ticket files to avoid conflicts with local ticket files. See the PostgreSQL Administrator's Guide for additional information on Kerberos.

Kerberos による認証 が使われいるときに限りPGREALM は 適用されます。 この環境変数がセットされている場合、 Postgres はこの領域にたいし サーバと認証をおこない、ローカルチケットファイルと 衝突を避けるため独立したチケットファイルを使用します。 Kerberos についての 追加情報は PostgreSQL 管理者ガイド を参照して下さい。

出力

psql returns 0 to the shell on successful completion of all queries, 1 for errors, 2 for abrupt disconnection from the backend. psql will also return 1 if the connection to a database could not be made for any reason.

全ての問合せが成功裡に終った場合、 psql はシェルに 0 を 返します。1 はエラーで、2 はバックエンドが急に 接続を解除した場合です。 psql は何らかの理由で データベースに接続できなかった場合にも 1 を返します。

The default TAB delimiter is used.

デフォルトとして TAB デリミタが使用されます。

説明

psql is a character-based front-end to Postgres. It enables you to type in queries interactively, issue them to Postgres, and see the query results.

psql は文字ベースの Postgres に対する フロントエンドです。 対話型で問合せを入力し、その問合せを Postgres にたいして発行、そして 結果を得ます。

psql is a Postgres client application. Hence, a postmaster process must be running on the database server host before psql is executed. In addition, the correct parameters to identify the database server, such as the postmaster host name, may need to be specified as described below.

psqlPostgres のクライアント アプリケーションです。ということは、 postmaster プロセスが データベースサーバホスト上で、psql が実行される以前に立ち上がっていなければなりません。 更に、postmaster ホスト名 のようなデータベースサーバを特定する正しいパラメータ が下記に説明するように指定されている必要があります。

When psql starts, it reads SQL commands from /etc/psqlrc and then from $(HOME)/.psqlrc This allows SQL commands like SET which can be used to set the date style to be run at the start of every session.

psql が起動するとき SQL コマンドを /etc/psqlrc から、 そして $(HOME)/.psqlrc から読み込みます。 セッションの開始毎に実行される日付形式の設定に使用する SET のようなコマンドを使うことも出来ます。

データベースへの接続

psql attempts to make a connection to the database at the hostname and port number specified on the command line. If the connection could not be made for any reason (e.g. insufficient privileges, postmaster is not running on the server, etc) .IR psql will return an error that says

     Connection to database failed.
    
The reason for the connection failure is not provided.

psql はコマンドラインで指定された ホスト名とポート番号上のデータベースに接続しようと試みます。 何らかの理由(例えば権限が無効であるとか、サーバ上で postmaster が 走っていない等)によって接続できなかった場合、 psql は、

     Connection to database failed.
    
のようにエラーを返します。 なぜ接続に失敗したかは通知されません。

問合せの入力

In normal operation, psql provides a prompt with the name of the database that psql is current connected to followed by the string "=>". For example,

通常の操作においては、 psql は、psql が現在接続しているデータベース名に続いて "=>" の文字列のプロンプト を表示します。 例えばこのようなものです。

$ psql testdb
Welcome to the POSTGRESQL interactive sql monitor:
  Please read the file COPYRIGHT for copyright terms of POSTGRESQL
[PostgreSQL 6.5.0 on i686-pc-linux-gnu, compiled by gcc 2.7.2.3]

   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: testdb
	  
testdb=>
    

At the prompt, the user may type in SQL queries. Unless the -S option is set, input lines are sent to the backend when a query-terminating semicolon is reached.

このプロンプトにたいして、ユーザは SQL 問合せが入力可能となります。 -S オプションが設定されていない限り、入力された行は一つの 問合せの終結を意味するセミコロンが入力された時点で、バック エンドに送られます。

Whenever a query is executed, psql also polls for asynchronous notification events generated by LISTEN and NOTIFY.

ある問合せが実行されたときはいつでも、 LISTENNOTIFY によって 生成された非同期通知イベントを psql は 同様に確認します。

psql can be used in a pipe sequence, and automatically detects when it is not listening or talking to a real tty.

psql はパイプ処理としても使用できます。 実ttyとの入出力がなければ自動的に(パイプであることを)検出します。

画面上でのページング

著者: From Brett McCormick on the mailing list 1998-04-04.

1998-04-04 メーリングリストの Brett McCormick より

To affect the paging behavior of your psql output, set or unset your PAGER environment variable. I always have to set mine before it will pause. And of course you have to do this before starting the program.

psql の出力のページングの振舞いをどう するかは、自分の PAGER の環境変数を設定または解除 します。 筆者はいつも中断する前に自分用のを設定しなければならなく なります。 もちろん、皆さんは psql プログラムを起動する前に行って おいてください。

In csh/tcsh or other C shells:

csh/tcsh または他の C シェルの場合:

% unsetenv PAGER
    
while in sh/bash or other Bourne shells: 一方 sh/bash または他の Bourne シェルの場合:
% unset PAGER
    

コマンドラインオプション

psql understands the following command-line options:

psql では以下のコマンドライン オプションが使えます。

-A

Turn off fill justification when printing out table elements.

テーブルの要素を出力するときの均等割り付けを行いません。

-c query

Specifies that psql is to execute one query string, query, and then exit. This is useful for shell scripts, typically in conjunction with the -q option in shell scripts.

psql が単一の問合せ文 query を実行し、そして終了するように指定します。 このことはシェルスクリプト、典型的な場合として、 スクリプト中の -q オプションと一緒に 使うと便利です。

-d dbname

Specifies the name of the database to connect to. This is equivalent to specifying dbname as the last field in the command line.

接続するデータベース名を指定します。 コマンドラインの最終フィールドで dbname を指定することと同等です。

-e

Echo the query sent to the backend

バックエンドへ送られた問合せをエコーします。

-E

Echo the actual query generated by \d and other backslash commands

\d で実際に生成された問合せと他のバックスラッシュの付いた コマンドをエコーします。

-f filename

Use the file filename as the source of queries instead of reading queries interactively. This file must be specified for and visible to the client frontend.

対話的に問合せを読み込むのではなしに、ファイル filename を 問合せの元として使用します。ファイルはクライアントフロント エンド用に、かつクライアントフロントエンドが処理できるよう 指定されていなければなりません。

-F separator

Use separator as the field separator. The default is an ASCII vertical bar ("|").

separator をフィールドセパレータとして使用します。 デフォルトは ASCII の縦棒 ("|") です。

-h hostname

Specifies the host name of the machine on which the postmaster is running. Without this option, communication is performed using local Unix domain sockets.

postmaster が走っている 機械のホスト名を指定します。 このオプションが無い場合、通信にはローカル Unix ドメイン ソケットが使用されます。

-H

Turns on HTML 3.0 tabular output.

HTML 3.0 表形式出力を有効にします。

-l

Lists all available databases, then exit. Other non-connection options are ignored.

利用できるデータベースの一覧を表示し、終了します。 他の接続関連以外のオプションは無視されます。

-n

Do not use the readline library for input line editing and command history.

入力行の編集とコマンド履歴にリードラインライブラリの使用を 禁止します。

-o filename

Put all output into file filename. The path must be writable by the client.

全ての出力をファイル filename に落します。ファイルへのパスはクライアントが書き込めるように なっていなければなりません。

-p port

Specifies the TCP/IP port or, by omission, the local Unix domain socket file extension on which the postmaster is listening for connections. Defaults to the value of the PGPORT environment variable, if set, or to 5432.

TCP/IP ポートを指定します。省略された場合は postmaster が接続を監視しているローカル Unix ドメインソケットファイル の拡張子となります。デフォルトは環境変数がセットされていれば PGPORT で、セットされていなければ 5432 です。

-q

Specifies that psql should do its work quietly. By default, it prints welcome and exit messages and prompts for each query, and prints out the number of rows returned from a query. If this option is used, none of this happens. This is useful with the -c option.

psql が沈黙して処理を行うよう指定します。 デフォルトでは welcome と exit メッセージ、それぞれの 問合せに対するプロンプトおよび問合せで戻った行数を 出力します。このオプションを使用するとこのように出力されません。 -c オプションと一緒に使うと便利です。

-s

Run in single-step mode where the user is prompted for each query before it is sent to the backend.

各問合せがバックエンドに送られる前に、 ユーザに確認のプロンプトが出されるシングルステップモードで 起動します。

-S

Runs in single-line mode where each query is terminated by a newline, instead of a semicolon.

各問合せがセミコロンではなく改行で締めくくられる様にして、 シングルステップモードで起動します。

-t

Turn off printing of column names. This is useful with the -c option in shell scripts.

カラム名の出力を無効にします。 シェルスクリプトでの -c オプションと一緒に使うと便利です。

-T table_options

Allows you to specify options to be placed within the table ... tag for HTML 3.0 tabular output.For example, border will give you tables with borders. This must be used in conjunction with the -H option.

HTML 3.0 表形式出力 用の table ... タグの中にオプションを 指定することを可能にします。 例えば、border はテーブルに枠を付けます。 このオプションは -H オプションと共に使用 する必要があります。

-u

Asks the user for the user name and password before connecting to the database. If the database does not require password authentication then these are ignored. If the option is not used (and the PGPASSWORD environment variable is not set) and the database requires password authentication, then the connection will fail. The user name is ignored anyway.

ユーザに対して、データベースに接続する前にユーザ名とパスワードを 求めます。データベースがパスワード認証を必要としてい なければ無視されます。このオプションが使用されないで (環境変数 PGPASSWORD が設定されておらず)データベースがパス ワード認証を必要とする場合、接続は失敗となります。いずれに してもユーザ名は無視されます。

-x

Turns on extended row format mode. When enabled each row will have its column names printed on the left with the column values printed on the right. This is useful for rows which are otherwise too long to fit into one screen line. HTML row output supports this mode also.

拡張行フォーマットモードを有効にします。 有効となっている場合、行のカラム名は左詰めで、カラムの値は 右詰めで出力されます。画面の横一列に何としても入らない長い 行に対して有効です。HTML の行出力もこのモードをサポート しています。

You may set environment variables to avoid typing some of the above options. See the section on environment variables below.--

上に述べたいくつかのオプションをいちいち入力することなしに 環境変数で設定することも出来ます。下記の環境変数の節を 参照して下さい。

psql メタコマンド

Anything you enter in psql that begins with an unquoted backslash is a psql meta-command. Anything else is SQL and simply goes into the current query buffer (and once you have at least one complete query, it gets automatically submitted to the backend). psql meta-commands are also called slash commands.

引用符なしでバックスラッシュから始まるいかなる psql への入力は、 psql のメタコマンドです。 どれもが SQL で単に現在の問合せバッファ に送られます。(そして、問合せ文が終結した時点でバッファの 内容は自動的にバックエンドに送付されます。) psql のメタコマンドはスラッシュコマンド とも呼ばれます。

The format of a psql command is the backslash, followed immediately by a command verb, then any arguments. The arguments are separated from the command verb and each other by any number of white space characters.

psql コマンドはバックスラッシュに すぐ続く動詞コマンドと、それに続く何らかの引数からなります。 引数は動詞コマンドからは離されて、それぞれがいくつでもよい 空白文字で区切られます。

With single character command verbs, you don't actually need to separate the command verb from the argument with white space, for historical reasons. You should anyway.

歴史的理由で、一文字の動詞コマンドは引数との間を空白文字で区切る 必要がありません。 ともかく次の通りです。

The following meta-commands are defined:

以下のメタコマンドが定義されています:

\a

Toggle field alignment when printing out table elements.

テーブルの要素を出力する時にフィールドの位置揃えをトグルします。

\C caption

Set the HTML3.0 table caption to "caption".

HTML3.0 のテーブルの見出しを "caption" に設定します。

\connect dbnameceable> [ username ]

Establish a connection to a new database, using the default username if none is specified. The previous connection is closed.

特に指定されていなければ、デフォルトの username で新規データベースに接続します。

\copy tableceable> { FROM | TO } filename

Perform a frontend (client) copy. This is an operation that runs a SQL COPY command, but instead of the backend reading or writing the specified file, and consequently requiring backend access and special user privilege, psql reads or writes the file and routes the data to or from the backend. The default tab delimiter is used.

フロントエンド(クライアント)のコピーを実行します。 SQL COPY コマンドの操作となります。 ただし、バックエンドが直接指定ファイルの読み書きを行なう(これ には必然的にバックエンドへのアクセスと特別なユーザ権限が要求さ れます)のではなく、psql がそのファ イルを読みバックエンドにそのデータを中継する、あるいは、バック エンドから中継されたデータをファイルに書き込みます。

Tip: This operation is not as efficient as the SQL COPY command because all data must pass through the client/server IP or socket connection. For large amounts of data this other technique may be preferable.

この操作は SQL COPY コマンド程 効率よくありません。というのは、全てのデータがクライアント/サーバ IP 接続かソケット接続を通じてやりとりされなくてはならないからです。 データ量が多いときは他の手法を使ったほうがよいでしょう。

\d [ table ]

List tables in the database, or if table is specified, list the columns in that table. If table name is specified as an asterisk ("*"), list all tables and column information for each tables.

データベースにあるテーブルの一覧を表示し、 table が指定 された場合は、テーブル内のカラムが表示されます。 星印 ("*") でテーブル名が指定されると 全てのテーブルとそれぞれのテーブルのカラムの情報の 一覧が表示されます。

\da

List all available aggregates.

有効な集約の一覧を表示します。

\dd object

List the description from pg_description of the specified object, which can be a table, table.column, type, operator, or aggregate.

pg_descriptionから, 指定された、テーブル、テーブル・カラム、型、演算子または 集約などのオブジェクトの記述の一覧を表示します。

Tip: Not all objects have a description in pg_description. This meta-command can be useful to get a quick description of a native Postgres feature.

全てのオブジェクトが pg_description に 記述があるとは限りません。 このメタコマンドは Postgres 本来の特徴の記述をを素早く見たいときに便利です。

\df

List functions.

関数の一覧を表示します。

\di

List only indexes.

インデックスのみの一覧を表示します。

\do

List only operators.

演算子の一覧のみを表示します。

\ds

List only sequences.

シーケンスの一覧のみを表示します。

\dS

List system tables and indexes.

システムテーブルとインデックスの一覧を表示します。

\dt

List only non-system tables.

非システムテーブルのみの一覧を表示します。

\dT

List types.

型の一覧を表示します。

\e [ filename ]

Edit the current query buffer or the contents of the file filename.

現在の問合せバッファまたは filename ファイルの 内容を編集します。

\E [ filename ]

Edit the current query buffer or the contents of the file filename and execute it upon editor exit.

現在の問合せバッファまたは filename ファイルの 内容を編集し、エディタが終了した時点で実行します。

\f [ separator ]

Set the field separator. Default is a single blank space.

フィールドのセパレータを設定します。デフォルトは 一つの空白スペースです。

\g [ { filename | |command } ]

Send the current query input buffer to the backend and optionally save the output in filename or pipe the output into a separate Unix shell to execute command.

現在の問合せ入力バッファをバックエンドに送り、オプションとして filename に出力を セーブするかまたは command 実行のため独立した Unix シェルに出力をパイプします。

\h [ command ]

Give syntax help on the specified SQL command. If command is not a defined SQL command (or is not documented in psql), or if command is not specified, then psql will list all the commands for which syntax help is available. If command is an asterisk ("*"), then give syntax help on all SQL commands.

指定した SQL コマンドの構文のヘルプを表示します。 command が SQL コマンドに定義されていない (あるいはpsqlにドキュメント化されていない) 場合または、command が 指定されていない場合、psql は構文の ヘルプが有効な全てのコマンドの一覧を表示します。

\H

Toggle HTML3 output. This is equivalent to the -H command-line option.

HTML3 出力をトグルします。 コマンドラインの -H オプションと同等です。

\i filename

Read queries from the file filename into the query input buffer.

問合せを filename ファイルから問合せ入力バッファに読み込みます。

\l

List all the databases in the server.

サーバに存在する全てのデータベースの一覧を表示します。

\m

Toggle the old monitor-like table display, which includes border characters surrounding the table. This is standard SQL output. By default, psql includes only field separators between columns.

テーブルを囲む線付の、旧式のモニタ形式のテーブル表示をトグルします。 これは標準の SQL 出力です。 デフォルトでは、psql はカラム間の フィールドセパレータのみ表示します。

\o [ { filename | |command } ]

Save future query results to the file filename or pipe future results into a separate Unix shell to execute command. If no arguments are specified, send query results to stdout.

後の問合せ結果を filename ファイルにセーブするか、 command 実行のため、後の問合せ結果を独立した Unix シェルにパイプします。

\p

Print the current query buffer.

現在の問合せバッファを表示します。

\q

Quit the psql program.

psql プログラムを終了します。

\r

Reset(clear) the query buffer.

問合せバッファをリセット(消去)します。

\s [ filename ]

Print or save the command line history to filename. If filename is omitted, do not save subsequent commands to a history file. This option is only available if psql is configured to use readline.

コマンドラインの履歴を表示するか filename に セーブします。 filename が省略 されると、引き続くコマンドは履歴ファイルにセーブされません。 このオプションは psql が リードライン を使用するように configure されているときのみ有効です。

\t

Toggle display of output column name headings and row count footer (defaults to on).

カラム名の書き出しと行数フッタの出力表示をトグルします。 (デフォルトは on です。)

\T table_options

Allows you to specify options to be placed within the table ... tag for HTML 3.0 tabular output.For example, border will give you tables with borders. This must be used in conjunction with the \H meta-command.

HTML 3.0 表形式出力 用の table ... タグの中にオプションを 指定することを可能にします。 例えば、border はテーブルに枠を付けます。 このオプションは \H メタコマンドと共に使用 されなければなりません。

\x

Toggles extended row format mode. When enabled each row will have its column names printed on the left with the column values printed on the right. This is useful for rows which are otherwise too long to fit into one screen line. HTML row output mode supports this flag too.

拡張行フォーマットモードをトグルします。 有効となっている場合、行のカラム名は左詰めで、カラムの値は 右詰めで出力されます。画面の横一列に何としても入らない長い 行に対して有効です。HTML の行出力もこのフラグをサポート しています。

\w filename

Outputs the current query buffer to the file filename.

現在の問合せバッファをファイル filename に出力します。

\z

Produces a list of all tables in the database with their appropriate ACLs (grant/revoke permissions) listed.

データベースにある全てのテーブルの一覧適切な ACL (grant/revoke パーミッション)リストをつけて表示します。

\! [ command ]

Escape to a separate Unix shell or execute the Unix command command.

独立した Unix シェルまたは Unix コマンド command へのエスケープです。

\?

Get help information about the slash ("\") commands.

スラッシュ ("\") コマンドに付いての ヘルプ情報を表示します。