Updated on 2025-05-29 GMT+08:00

Importing and Exporting Data Files on a GSQL Client

When you execute the COPY command to export data on a GSQL client, the generated data files are saved on the database server by default. This can make it difficult for users to access the files. To offer easier file access, the \COPY command directly generates data files on the local client.

Differences Between COPY and \COPY

  1. File location: The files generated and read during COPY import are located on the server. Conversely, in \COPY import, both the generated and read files are located on the client.
  2. Performance: To complete \COPY import, the client must read file streams and transmit them to the server. This results in decreased performance in comparison to COPY.
  3. Function: Compared to COPY, \COPY offers an additional capability—parallel import on clients. For detailed specifications, consult "Database Connection Tools > gsql for Connecting to a Database > Meta-Command Reference" in Tool Reference.

Example \COPY Commands

The difference between the export commands of \COPY and COPY is that COPY is replaced by \COPY. Below is a simple example of converting CSV export commands from COPY to their \COPY equivalents:

1
2
3
4
5
6
-- COPY commands
COPY {data_source} to '/path/export.csv' encoding {server_encoding} csv;
COPY {data_source} from '/path/export.csv' encoding {server_encoding} csv;
-- Corresponding \COPY commands
\COPY {data_source} to '/path/export.csv' encoding {server_encoding} csv;
\COPY {data_source} from '/path/export.csv' encoding {server_encoding} csv;

Example Commands for Parallel Import

1
2
3
4
5
6
7
8
9
-- Command for import in CSV format
\COPY {data_destination} from '/path/export.txt' encoding {file_encoding} parallel {parallel_num} csv;
-- Command for import in FIXED format
\COPY {data_destination} from '/path/export.txt' encoding {file_encoding} parallel {parallel_num} fixed;
-- Command for import in TEXT format
\COPY {data_destination} from '/path/export.txt' encoding {file_encoding} parallel {parallel_num};
-- data_destination can only be a table name.
-- file_encoding indicates the encoding format used during binary file export.
-- parallel_num indicates the number of clients for data import. When there are sufficient cluster resources, it is advisable to set it to 8.