1
1

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: デリミタをタブにしてインポート・エクスポート

1
Last updated at Posted at 2020-11-22

PostgreSQL でデリミタをタブにしてインポート・エクスポートをするコマンド

エクスポート

psql -h host_name -U user_name db_name -c "SELECT * FROM table_name WHERE condition;" -AF $'\t' --no-align > file_name.tsv

SQLの実行結果に絞ってエクスポートできます。

CSV のようにタブ区切りになりますが、 実際は CSV ではなく SQL の実行結果です。

インポート

psql -h host_name -U user_name db_name -c "\copy table_name FROM 'file_name.tsv' DELIMITER E'\t' CSV HEADER"

COPY でファイルから読み込むことはスーパーユーザにしかできません。 \copy を使います。

HEADER は1行目がヘッダであることを示します。 インポートするときは1行目をヘッダとしてインポートの対象から除きます。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?