The gsql tool of GaussDB(DWS) provides the \copy meta-command to import data.
For details about the \copy command, see Table 1.
Syntax |
Description |
---|---|
\copy { table [ ( column_list ) ] | ( query ) } { from | to } { filename | stdin | stdout | pstdin | pstdout } [ with ] [ binary ] [ oids ] [ delimiter [ as ] 'character' ] [ null [ as ] 'string' ] [ csv [ header ] [ quote [ as ] 'character' ] [ escape [ as ] 'character' ] [ force quote column_list | * ] [ force not null column_list ] ] |
You can run this command to import or export data after logging in to the database on any gsql client. Different from the COPY statement in SQL, this command performs read/write operations on local files rather than files on database servers. The accessibility and permissions of the local files are restricted to local users. NOTE:
\copy only applies to small-batch data import with uniform formats but poor error tolerance capability. GDS or COPY is preferred for data import. |
Specifies the name (possibly schema-qualified) of an existing table.
Value range: an existing table name
Specifies an optional list of columns to be copied.
Value range: any field in the table. If the column list is not specified, all columns in the table will be copied.
Specifies that the results will be copied.
Valid value: a SELECT or VALUES command in parentheses.
Specifies the absolute path of a file. To run the \copy command, the user must have the write permission for this path.
Specifies that data is stored and read in binary mode instead of text mode. In binary mode, you cannot declare DELIMITER, NULL, or CSV. After specifying BINARY, CSV, FIXED and TEXT cannot be specified through option or copy_option.
Specifies the internal OID to be copied for each row.
An error is raised if OIDs are specified for a table that does not have OIDs, or in the case of copying a query.
Valid value: true, on, false, and off.
Default value: false
Value range: a multi-character delimiter within 10 bytes.
Default value:
Specifies that a string represents a null value in a data file.
Value range:
Default value:
Specifies whether a data file contains a table header. header is available only for CSV and FIXED files.
In data import scenarios, if header is on, the first row of the data file will be identified as the header and ignored. If header is off, the first row will be identified as a data row.
If header is on, fileheader must be specified. fileheader specifies the content in the header. If header is off, the exported file does not contain a header.
Valid value: true, on, false, and off.
Default value: false
Specifies the quote character for a CSV file.
Default value: double quotation mark ("").
This option is allowed only when using CSV format. This must be a single one-byte character.
Default value: double quotation mark (""). If the value is the same as the quote value, it will be replaced with \0.
In CSV COPY TO mode, forces quoting to be used for all not-null values in each specified column. NULL will not be quoted.
Value range: an existing column.
In CSV COPY FROM mode, processes each specified column as though it were quoted and hence not a null value.
Value range: an existing column.
1 2 3 4 5 6 7 8 | create table copy_example ( col_1 integer, col_2 text, col_3 varchar(12), col_4 date, col_5 time ); |
1 | \copy copy_example from stdin csv; |
1 2 3 4 | Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> 1,"iamtext","iamvarchar",2006-07-07,12:00:00 >> \. |
iamheader
1|"iamtext"|"iamvarchar"|2006-07-07|12:00:00
2|"iamtext"|"iamvarchar"|2022-07-07|19:00:02
1 | \copy copy_example from '/local/data/example.csv' with(header 'on', format 'csv', delimiter '|', date_format 'yyyy-mm-dd', time_format 'hh24:mi:ss'); |
1,"iamtext","iamvarchar",2006-07-07
2,"iamtext","iamvarchar",2022-07-07,19:00:02,12:00:00
Import data from the local file example.csv to the target table copy_example. The default delimiter is (,). Therefore, you do not need to specify the delimiter. Because the fault tolerance parameters IGNORE_EXTRA_DATA and FILL_MISSING_FIELD are specified, the missing fields will be replaced with NULL, the extra fields are ignored.
1 | \copy copy_example from '/local/data/example.csv' with( format 'csv', date_format 'yyyy-mm-dd', time_format 'hh24:mi:ss', IGNORE_EXTRA_DATA 'true', FILL_MISSING_FIELD 'true'); |
1 | \copy copy_example to stdout CSV quote as '"' force quote col_4,col_5; |