管理人Kのひとりごと

デジモノレビューやプログラミングや写真など

PostgreSQLでスキーマ操作関連

PostgreSQLでのスキーマ関連操作のメモ。スキーマはオブジェクトをグループ化する仕組みといったところか。

postgres=# select version();
                                         version
------------------------------------------------------------------------------------------
 PostgreSQL 9.6.4 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
-- スキーマを作成する
CREATE SCHEMA sch_sales;
-- スキーマ一覧を表示する
postgres=# \dn
     スキーマ一覧
   名前    |  所有者
-----------+----------
 public    | postgres
 sch_sales | postgres
(2 行)||<
>|sql|
-- パブリックスキーマから sch_sales へテーブルを移動する
alter table wk_order set schema sch_sales;
-- 現在のスキーマを確認
select current_schema();
-- スキーマを切り替える
SET search_path = sch_sales;
-- スキーマ内のテーブル一覧を表示
postgres=# \dt
             リレーションの一覧
 スキーマ  |   名前   |    型    |  所有者
-----------+----------+----------+----------
 sch_sales | wk_order | テーブル | postgres