PostgreSQL Error – Fixing ERROR: COPY Format “CSV” Not Recognized

copyerrorspostgresqlpsql

Whever I try to copy from

test=# \COPY table FROM 'file.csv' WITH ( FORMAT 'CSV', HEADER true );
ERROR:  COPY format "CSV" not recognized

What's the reason for this? I know I've used this before.

Best Answer

If format is a string, the string is case sensitive,

test=# \COPY table FROM 'file.csv' WITH ( FORMAT 'csv', HEADER true );

But, the format doesn't have to be a string,

test=# \COPY table FROM 'file.csv' WITH ( FORMAT CSV, HEADER true );

You're probably use to using the non-stringified version. The syntax for the copy_options is pretty hectic,

FORMAT format_name
OIDS [ boolean ]
FREEZE [ boolean ]
DELIMITER 'delimiter_character'
NULL 'null_string'
HEADER [ boolean ]
QUOTE 'quote_character'
ESCAPE 'escape_character'
FORCE_QUOTE { ( column_name [, ...] ) | * }
FORCE_NOT_NULL ( column_name [, ...] )
FORCE_NULL ( column_name [, ...] )
ENCODING 'encoding_name'

A lot of them only accept strings, but some of them accept regular tokens which get lowercased.

This specific thing isn't documented explicitly but you can find more information about COPY here