–tableオプションとは
pg_dumpの-Fc(カスタムフォーマット)で取得したダンプは、pg_restoreの--table(-t)オプションで特定のテーブルのみをリストアできる。プレインテキスト形式のダンプは先頭から順にSQL文を実行する仕組みのため、リストア時にテーブルを絞り込むことはできない。カスタムフォーマットであれば、全テーブルを含む1つのダンプファイルを作成しておき、リストアするタイミングで必要なテーブルだけを選べる。
以下のテーブルを持つデータベースでダンプを取得し、動作を確認する。
CREATE TABLE items (
id integer PRIMARY KEY,
name text NOT NULL,
price integer NOT NULL
);
CREATE TABLE categories (
id integer PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE orders (
id integer PRIMARY KEY,
item_id integer NOT NULL REFERENCES items(id)
);
$ pg_dump -U postgres -Fc mydb -f dump.custom
特定のテーブルのみリストアする
--tableに対象のテーブル名を指定すると、そのテーブルのみリストアできる。
$ pg_restore -U postgres -d restoredb --table=items dump.custom
$ psql -U postgres -d restoredb -c "\dt"
Schema | Name | Type | Owner
--------+-------+-------+----------
public | items | table | postgres
categoriesやordersを含むダンプからitemsテーブルのみが復元されている。
--tableは複数回指定でき、複数のテーブルをまとめてリストアできる。
$ pg_restore -U postgres -d restoredb --table=items --table=categories dump.custom
List of relations
Schema | Name | Type | Owner
--------+------------+-------+----------
public | categories | table | postgres
public | items | table | postgres
外部キー制約があるテーブルも単独でリストアできる
ordersテーブルはitemsテーブルを参照する外部キー制約を持つが、参照先のitemsテーブルをリストアしなくてもorders単独でリストアできる。
$ pg_restore -U postgres -d restoredb2 --table=orders dump.custom
$ psql -U postgres -d restoredb2 -c "\d orders"
Table "public.orders"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
id | integer | | not null |
item_id | integer | | not null |
エラーにならずordersテーブルは作成されるが、外部キー制約orders_item_id_fkeyはリストアされていない。同様に主キー制約orders_pkeyも付与されていない。--tableはテーブル本体とそのデータのみを対象とし、制約やインデックスは別のTOCエントリとして扱われるため、指定したテーブルに含まれない。
制約も含めてリストアする
制約も含めてリストアしたい場合は、pg_restore -lで出力したTOC(Table of Contents)を編集し、-L(--use-list)オプションで使う方法がある。
まずTOCをファイルに出力する。
$ pg_restore -l dump.custom > toc.list
;
; Archive created at 2026-07-25 00:24:38 UTC
; dbname: postgres
; TOC Entries: 15
; Compression: gzip
; Format: CUSTOM
;
; Selected TOC Entries:
;
216; 1259 16391 TABLE public categories postgres
215; 1259 16384 TABLE public items postgres
217; 1259 16398 TABLE public orders postgres
3425; 0 16391 TABLE DATA public categories postgres
3424; 0 16384 TABLE DATA public items postgres
3426; 0 16398 TABLE DATA public orders postgres
3277; 2606 16397 CONSTRAINT public categories categories_pkey postgres
3275; 2606 16390 CONSTRAINT public items items_pkey postgres
3279; 2606 16402 CONSTRAINT public orders orders_pkey postgres
3280; 2606 16403 FK CONSTRAINT public orders orders_item_id_fkey postgres
toc.listからitemsテーブルに関するエントリ(テーブル本体、データ、主キー制約)以外の行を削除する。コメント行(;で始まる行)はそのまま残す。
;
; Archive created at 2026-07-25 00:24:38 UTC
; dbname: postgres
; TOC Entries: 15
; Compression: gzip
; Format: CUSTOM
;
; Selected TOC Entries:
;
215; 1259 16384 TABLE public items postgres
3424; 0 16384 TABLE DATA public items postgres
3275; 2606 16390 CONSTRAINT public items items_pkey postgres
編集したTOCを-Lで指定してリストアする。
$ pg_restore -U postgres -d restoredb3 -L toc.list dump.custom
$ psql -U postgres -d restoredb3 -c "\d items"
Table "public.items"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | not null |
name | text | | not null |
price | integer | | not null |
Indexes:
"items_pkey" PRIMARY KEY, btree (id)
--tableでは付与されなかった主キー制約items_pkeyが、-Lで指定したTOCエントリ通りにリストアされている。特定のテーブルを制約ごと復元したい場合は、--tableではなく-LでTOCを編集する方法を使う。
まとめ
pg_dumpの-Fcで取得したダンプは、pg_restore --tableで特定のテーブルのみをリストアできる。ただし対象はテーブル本体とデータのみで、主キーや外部キーなどの制約は含まれない。制約も含めて特定のテーブルを復元したい場合は、pg_restore -lで出力したTOCを編集し、-Lオプションで対象エントリを指定する。
