Reference: Copy Statement
Copy statements allow bulk im- and export of tables:
Usage example:
-- Export
copy (select * from movies) to 'movies_backup.csv';
-- Import
copy movies2 from 'movies_backup.csv';Options
Copy supports a number of options controlling the format.
Format:csv,text,binary. Only for copy to:numpy.binaryuses PostgreSQL’s binaryCOPYformat, which starts with thePGCOPYsignature, and works withSTDINandSTDOUT.Delimiter: The record separator fortextandcsvformats. By default\t, and,.Header: If thetextandcsvhas a header. By defaultfalse.Null: The string that represents anullvalue intextandcsvformats. By default\N, and empty string.Quote: The quoting character for thecsvformat. By default". This can be used to insert values containing the delimiter.Escape: The escape character for thecsvformat. By default". This can be used to escape the quote character.Force Not Null: Do not match the specified columns’ values against the null string. In the default case where the null string is empty, this means that empty values will be read as zero-length strings rather than nulls, even when they are not quoted. If*is specified, the option will be applied to all columns. This option is allowed only inCOPY FROM, and only when usingcsvformat.Force Null: Match the specified columns’ values against the null string, even if it has been quoted, and if a match is found set the value toNULL. In the default case where the null string is empty, this converts a quoted empty string into NULL. If*is specified, the option will be applied to all columns. This option is allowed only inCOPY FROM, and only when usingcsvformat.Note:FORCE_NULLandFORCE_NOT_NULLcan be used simultaneously on the same column. This results in converting quoted null strings to null values and unquoted null strings to empty strings.
CedarDB also supports a best-effort import mode:
(on_error ignore): By default, Copy aborts when encountering invalid rows. With this option, rows containing mismatching elements will be skipped on a best-effort basis.
A query that uses all available copy options could look like this:
COPY target_table (column1, column2, column3)
FROM '/absolute/path/to/your_file.csv'
WITH (
FORMAT csv,
DELIMITER ',',
HEADER true,
NULL 'null',
QUOTE '"',
ESCAPE '\',
FORCE_NOT_NULL (column1, column2),
FORCE_NULL (column3),
ON_ERROR ignore
);Permissions
COPY FROM requires the INSERT privilege on the table, and COPY TO requires the SELECT privilege on the table or the privileges to run the query.
Both also require USAGE on the schema of the table.
Reading or writing files on the server additionally requires superuser, or membership in pg_read_server_files (for COPY FROM) or pg_write_server_files (for COPY TO).
Otherwise, CedarDB rejects the statement with file access denied.
COPY ... FROM STDIN and COPY ... TO STDOUT, e.g., via psql’s \copy, do not need file access.
PostgreSQL Differences
FORMAT binaryis not supported forCOPY TOa file.COPY ... TO STDOUTandCOPY ... FROM STDINsupport it.COPY ... WHERE,FORCE_QUOTE, theDEFAULToption, andCOPY FROM PROGRAMare not supported.COPY TO PROGRAMonly prints a warning and does not run the program.ON_ERROR ignoreskips rows that fail to parse, instead of stopping.HEADERhas no effect forCOPY TOintextformat.HEADER matchdoes not skip or check the header line.COPY TOandCOPY FROMaccept relative file paths and resolve them against the server’s working directory. PostgreSQL rejects relative paths forCOPY TO.