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?

psqlコマンドからINSERT文に使用できるCSVデータをエクスポートする

0
Last updated at Posted at 2026-07-13

概要

ほぼ無いと思いますが、SQLクライアントが使えない場合にバックアップを取得します。
サクラエディタで正規表現を使い、先頭に,(、末尾に)を加えて、
VALUESに指定した挿入予定の先頭レコードの,を削除すれば完成です。

psql -fに送るSQLを含むコマンド

export.sql
\echo 'INSERT INTO <テーブル名> VALUES'
COPY (
    SELECT *
    FROM <テーブル名>
    WHERE ...
    ORDER BY ...
) TO STDOUT
WITH (FORMAT CSV, NULL 'NULL', DELIMITER ',', QUOTE '''', FORCE_QUOTE *);
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?