Using COPY command
- Last Updated: June 25, 2021
- 2 minute read
- DataDirect Connectors
- JDBC
- PostgreSQL 6.0
- Documentation
The driver supports a customized version of the PostgreSQL COPY command. It
provides an additional keyword, LOCALFILE, to allow
you to copy data from or to standard file-system files that are stored anywhere on your
network, not just on the database server. The file types supported by the driver are
.txt and .csv.
Note: The customized COPY command supports all the
options supported by the PostgreSQL COPY command.
Syntax
To copy data from a file to an existing table in the database, use the following syntax:
COPY tablename[(columnname[,...])] from {LOCALFILE 'filepath'} [[WITH](option[,...])]
where:
- tablename
- is the name of the table that you are copying data to.
- columnname
- is the name of the column available in the table you are copying data to.
- filepath
- is the absolute path of the file you are copying data from.
- option
- can be one of the following:
FORMAT,OIDS,DELIMITER,NULL,HEADER,QUOTE,ESCAPE,FORCE_QUOTE,FORCE_NOT_NULL, andENCODING.
Example:
COPY testcopytable(intcol, varcharcol, charcol) from {LOCALFILE 'C:\\Users\\abc\\data.txt'} with DELIMITER ','To copy data from an existing table in the database to a file, use the following syntax:
COPY tablename[(columnname[,...])] to {LOCALFILE 'filepath'} [[WITH](option[,...])]
where:
- tablename
- is the name of the table that you are copying data from.
- columnname
- is the name of the column available in the table you are copying data from.
- filepath
- is the absolute path of the file you are copying data to.
- option
- can be one of the following:
FORMAT,OIDS,DELIMITER,NULL,HEADER,QUOTE,ESCAPE,FORCE_QUOTE,FORCE_NOT_NULL, andENCODING.
Example:
COPY testcopytable(intcol, varcharcol, charcol) to {LOCALFILE '/home/users/abc/copydata.csv'} with DELIMITER ','