0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Postgresql] Dumpして別なDBにデータを挿入する方法

Posted at

任意のテーブルのデータを、ローカルDBに挿入しなければならない機会があったのでメモ。
※DBの向き先には注意する。どのDBからデータをDumpして、どのDBにデータを流すのかを間違えないようにしよう。

任意のテーブルのデータをDump

$ pg_dump -t table -a db > output_file

# e.g.
pg_dump -t users -a hoge_development > users.sql

使用しているoptionの意味

option 意味
-t [table]
--table=[table]
Dumpするテーブルを指定
-a
--data-only
データのみをDumpする(schemaはDumpしない)

Dumpしたデータを挿入する

$ psql db < sql

# e.g.
psql hoge_development < users.sql

その他

pg_dump コマンドのオプションの指定方法で少しハマった。


# NG
$ pg_dump -t -a users hoge_development > users.sql

# NG
$ pg_dump -ta users hoge_development > users.sql

# OK
$ pg_dump -t users -a hoge_development > users.sql

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?