56
54

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.

postgresでcsv出力

Posted at

postgresからcsv出力するメモ
COPYでのファイル名指定は、DBサーバから見たパスになるため注意

COPY TO

# csv形式での出力
# 区切り文字、QUOTE文字、NULL文字列、復帰、改行文字を含む値の場合はダブルクォーテーションつく
$ psql TESTDB -c "COPY (select * from foo) TO '/tmp/foo.csv' (FORMAT csv)"

# 全項目(null値以外) ダブルクォーテーションつく
$ psql TESTDB -c "COPY (select * from foo) TO '/tmp/foo.csv' (FORMAT csv, FORCE_QUOTE *)"

psqlコマンド

https://www.postgresql.jp/document/9.3/html/app-psql.html
ダブルクォーテーションつけたい場合はselect文で自前で付与しないとダメ??

# ダブルクォーテーションつかない
$ psql TESTDB -c "select * from foo" -A -F, > /tmp/foo.csv
  • -A = 位置揃えなし
  • -F, = カンマ区切り
56
54
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
56
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?