UJP - 技術情報1

Life is fun and easy!

不正IP報告数

Okan Sensor
 
メイン
ログイン
ブログ カテゴリ一覧

PostgreSQLの対話型ツールpsqlを利用する

PostgreSQLの対話型ツールpsqlを利用する


0.改訂履歴

  • 2005.09.29 新規作成

1.はじめに

 このドキュメントでは,PostgreSQL7.4.6にて,対話型ツールのpsqlを利用する. 対話型ツールとは,OracleのSQL*Plus,Sybaseのisql等のようなものである. ここでのサンプルとして,データベース内の情報,たとえばデータベース,テーブル一覧や定義情報等で,管理上に最低限必要なメタコマンドなどの紹介も行う.

2.対話型ツールpsqlを使ってデータベースに接続する

  • まずは,何がともあれ,使ってみる.
[postgres@mars postgres]$ psql -U steve MyTestDB
Welcome to psql 7.4.6, the PostgreSQL interactive terminal.

Type:  ¥copyright for distribution terms
       ¥h for help with SQL commands
       ¥? for help on internal slash commands
       ¥g or terminate with semicolon to execute query
       ¥q to quit

MyTestDB=>  
  • この例では,ユーザsteveを使って,MyTestDBというデータベースに接続している.
  • 現在時刻を表示する関数を実行してみる.
MyTestDB=>  select now();
              now              
-------------------------------
 2005-09-14 19:11:13.903592+09
(1 row)

MyTestDB=> 
  • psqlを終了する.
MyTestDB=> quit
MyTestDB-> ¥q
[postgres@mars postgres]$  
  • quitではだめで,¥qで終了する.

3.データベース一覧のリストを取得する

  • データベースに接続する際に,どのようなデータベースがあるかを確認する必要が
[postgres@mars postgres]$ psql -U postgres -l
        List of databases
   Name    |  Owner   | Encoding 
-----------+----------+----------
 MyTestDB  | postgres | EUC_JP
 template0 | postgres | EUC_JP
 template1 | postgres | EUC_JP
(3 rows)

[postgres@mars postgres]$  
  • 再度データベースに接続して,¥listメタコマンドを利用する.
[postgres@mars postgres]$ psql -U steve MyTestDB
Welcome to psql 7.4.6, the PostgreSQL interactive terminal.

Type:  ¥copyright for distribution terms
       ¥h for help with SQL commands
       ¥? for help on internal slash commands
       ¥g or terminate with semicolon to execute query
       ¥q to quit

MyTestDB=> ¥list
        List of databases
   Name    |  Owner   | Encoding 
-----------+----------+----------
 MyTestDB  | postgres | EUC_JP
 template0 | postgres | EUC_JP
 template1 | postgres | EUC_JP
(3 rows)

MyTestDB=>  
  • 同じ結果が得られることが確認できた.

4.データベースユーザを確認する

  • メタコマンド¥duを使って,ユーザリストを取得する.
MyTestDB=> ¥du

					              List of database users

					 User name | User ID |         Attributes 

					-----------+---------+----------------------------
postgres  | 1       | superuser, create database
steve     | 100     | create database

					(2 rows)


					MyTestDB=> 
  • 保有している権限についても確認できる.

5.テーブル一覧,インデックス一覧を確認する

  • メタコマンド¥dを使って,現在のデータベースにあるテーブル一覧を取得する.
MyTestDB=> ¥d
No relations found.
MyTestDB=>
  • データベースを作成したばかりだと何もない.
  • 仮のテーブルを作成して一覧を表示する.
MyTestDB=> create table AAA (a int);
CREATE TABLE
MyTestDB=> ¥d
       List of relations
 Schema | Name | Type  | Owner 
--------+------+-------+-------
 public | aaa  | table | steve
(1 row)

MyTestDB=>  
  • 次に,メタコマンド¥diを利用してインデックス一覧を取得する.
  • これもインデックスを作成した後に表示させてみる.
MyTestDB=> create index index_aaa on aaa(a);
CREATE INDEX
MyTestDB=> ¥di
             List of relations
 Schema |   Name    | Type  | Owner | Table 
--------+-----------+-------+-------+-------
 public | index_aaa | index | steve | aaa
(1 row)

MyTestDB=>

6.テーブルの定義内容を各員する

  • テーブルがどのようなフィールドで構成されているかをメタコマンド¥dを使って確認する.
MyTestDB=> ¥d aaa;
      Table "public.aaa"
 Column |  Type   | Modifiers 
--------+---------+-----------
 a      | integer | 
Indexes:
    "index_aaa" btree (a)

MyTestDB=>  
  • このテーブルに関係しているインデックスも確認できる.

7.実行時間を表示させる.

  • SQL文を実行した際の実行時間を表示させる.
  • デフォルトでは表示されない.
MyTestDB=> select * from aaa;
 a 
---
(0 rows)

MyTestDB=> ¥timing
Timing is on.
MyTestDB=> select * from aaa;
 a 
---
(0 rows)

Time: 1.881 ms
MyTestDB=>  
  • この設定はトグルになっているので,
MyTestDB=> ¥timing
Timing is off.
MyTestDB=> 
  • もう一度実行するとこのようにオフになる.

8.アクセス権を指定する

  • アクセス権表示する
MyTestDB-> ¥z
Access privileges for database "MyTestDB"
 Schema | Table | Access privileges 
--------+-------+-------------------
 public | aaa   | 
(1 row)

MyTestDB-> 
  • テーブルに設定してあるアクセス権を表示させているが,ここでは何も設定されていない為,表示結果がない.


広告スペース
Google