Postgre のcsv出力でヘッダーを""で囲みたい
解決したいこと
postgresqlでcopy を使用してヘッダーをだぶるダブルクォーテーションで囲んで出力したい。
出力例)
.csv
"name","id","age"
"田中","01","20"
"高橋","02","22"
実行コードと結果
例1)
.sql
copy (select name as """name""" ,
id as """id""",
age as """age"""
from meibo_table)
to 'C:/tmp/meibo.csv' with csv header force quote *;
.csv
"""name""","""id""","""age"""
"田中","01","20"
"高橋","02","22"
例2)
.sql
copy (select name as "name" ,
id as "id",
age as "age"
from meibo_table)
to 'C:/tmp/meibo.csv' with csv header force quote *;
.csv
name,id,age
"田中","01","20"
"高橋","02","22"
例3)
.sql
copy (select name as """name" ,
id as """id",
age as """age"
from meibo_table)
to 'C:/tmp/meibo.csv' with csv header force quote *;
.csv
"""name","""id","""age"
"田中","01","20"
"高橋","02","22"
自分で試したこと
""で囲むために色々試したがうまくいかないです。
例1のselect句を実行すると出力されるカラム名はクォーテーションで囲まれるがcopy文内に入れるとクォーテーションが増殖します。
- "'name'"はシングルクォーテーションで囲まれる
- ""name""はerror
postgres 14?15?だった気がします。
0