PostgreSQL Windows – How to Save psql ERROR Output to Same File as DDL Commands

postgresqlwindows

I'm trying to restore a database from a text file. I'm following the answer on this question. How do I restore a plain text postgres .backup file

But add: > c:\result.txt

psql.exe -U  sde sde < c:\backup\sde_04_06_2018.backup > c:\backup\result.txt

This seem to work but doesn't write the errors.

You get messages like this on the text file:

REVOKE
GRANT
SET
ALTER DEFAULT PRIVILEGES
ALTER DEFAULT PRIVILEGES

But on the command windows you see error like this:

ERROR:  constraint "sde_layer_stats_version_id_fkey" for relation sde_layer_stats" already exists
ERROR:  constraint "xml_columns_fk1" for relation "sde_xml_columns" already exists
ERROR:  constraint "xml_columns_fk2" for relation "sde_xml_columns" already exists
ERROR:  constraint "xml_indextags_fk1" for relation "sde_xml_index_tags" already exists

The idea is save the error on the text file so I can review and decide if are something I need to handle.

enter image description here

Best Answer

I want help with how write the command line so I can save the output of the restore to a file.txt. So the create/insert/alter on the backup aren't relevant. And the image is just as reference, I also include the message as text just in case.

If you're asking how do you put the error to the "result file" too, you need to redirect STDERR as well as STDOUT or direct STDERR to STDOUT with 2>&1

psql.exe -U  sde sde < c:\backup\sde_04_06_2018.backup > c:\backup\result.txt 2>&1

Internally you have three "standard" streams: input, output, and error.

  • input you're directing that to c:\backup\sde_04_06_2018.backup
  • output you're directing output to c:\backup\result.txt
  • You need to also redirect error.