But is there a way to export it during the COPY process?
Streaming the COPY data into a program is supported through the PROGRAM
clause.
The Unix command split
can do the actual splitting. For instance:
COPY (<your query>) TO PROGRAM 'split -l 10000 -d - /path/file-';
This will split the output into files numbered sequentially that do not exceed 10000 lines each.
The PROGRAM
clause is also supported by psql's \copy
; in that case the program is executed client-side.
Caveat: if the export format is CSV and there are multi-line text fields, it comes with the risk of splitting a field across multiple files.