LoginSignup
5
2

More than 5 years have passed since last update.

PostgreSQLのpg_dumpとPowerShellの組み合わせでハマった話

Posted at

PowerShellからpg_dumpで作成したダンプファイルがリストア時にエラー

現象

pg_dump -d "DB名" > "hoge.dump"

PowerShellから上記のようなコマンドで作成したダンプファイルが、psqlでリストアする際にエラーが発生し失敗する。

ERROR: invalid byte sequence for encoding "SJIS": 0xff 0xfe

原因

PowerShellでは、リダイレクト(> or >>)を使ってテキストファイルにテキストを書き込むと、Unicode形式(UTF16)で書き込まれるため。

回避策

  • cmdシェルを使う
  • バイナリ形式でダンプする
  • 標準出力からファイルにリダイレクトするのではなく、pg_dumpの -f オプションを利用して直接ファイル出力する
  • パイプを使ってOut-Fileコマンドレットでファイルに出力する
pg_dump -E sjis -d "DB名" | Out-File -encoding default -filepath "hoge.dump"
5
2
1

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
5
2