postgresql copy from with csv array literal whose delimiter is ',' - postgresql

I'd like to copy from with csv file with Postgres.
That csv file has array literal whose delimiter is ;.
example: a,b,c,{1;2;3}
I did that with replacing delimiter of csv file , to | and set option delimiter | and replacing delimiter of array literal ; to ,.
example: a|b|c|{1,2,3}
I think there may be option to set delimiter of array literal.
If so, I don't have to replace delimiter of csv file.
Are there any smarter way?

There is no option to configure the separator between the elements of an array in its text representation.
But if you have any control over how the CSV file is generated, you can escape the array literal:
a,b,c,"{1,2,3}"
That would work fine with COPY.

Related

Postgres: How to use COPY FROM WITH DELIMITER with CHR function?

I try to import a text file into a single column table, i.e. I don't want a single line of the source file to be delimited into columns. The file contains many different characters (tabs, commas, spaces) that could be recognized as delimiters. Since bell (CHR(7)) doesn't exist in the data file I chose it as delimiter:
COPY data_table(single_column) FROM '/tmp/data' WITH ENCODING 'LATIN1' DELIMITER CHR(7);
Unfortunately, this results in an error:
ERROR: syntax error at or near "chr"
What would be the correct syntax?
You can't use a function there. Use the escape notation.
DELIMITER E'\007'

PostgreSQL exclude extra comma from being used as a delimiter while parsing csv file

I am trying to copy data from a csv file into my database table. But the problem is that a column named title has some values that contain a comma within.
How can I exclude it from being used as a delimiter while parsing?
Sample data:
"Hold On, I'm Coming", The Canettes Blues Band, On Tap & In the Can, 34, 100, 282
I don't want the comma after "Hold on" to be used as a delimiter.
If you are using copy or psql's \copy then use the options
format csv quote '"'
this will make the import ignore commas inside quoted values (which is what your sample data uses)
e.g. in psql
\copy target_table from the_input_file.txt with (format csv quote '"')

Postgres COPY command with literal delimiter

I was trying to import a CSV file into a PostgreSQL table using the COPY command. The delimiter of the CSV file is comma (,). However, there's also a text field with a comma in the value. For example:
COPY schema.table from '/folder/foo.csv' delimiter ',' CSV header
Here's the content of the foo.csv file:
Name,Description,Age
John,Male\,Tall,30
How to distinguish between the literal comma and the delimiter?
Thanks for your help.
To have the \ to be recognized as a escape character it is necessary to use the text format
COPY schema.table from '/folder/foo.csv' delimiter ',' TEXT
But then it is also necessary to delete the first line as the HEADER option is only valid for the CSV format.

COPY command and newline error

I have my simple CSV file, delimited by commas and no strange character, and when trying to do a \COPY table FROM '/srv/www/.../CSV.csv' WITH DELIMITER ',' I get the error
ERROR: literal newline found in data
HINT: Use "\n" to represent newline.
Its not the first time I execute a COPY and it never happened to me. I exported the CSV from excel to Notepad and saved here as CSV (I know some programs add extra info, but AFAIK notepad does not)
If you want to import CSV, you must specify FORMAT CSV. A literal newline is not legal in PostgreSQL's default delimited input.
\COPY table FROM '/srv/www/.../CSV.csv' WITH (FORMAT CSV, DELIMITER ',');

Trying to import a CSV file into postgres with comma as delimeter

I am trying to import a CSV file into Posgres that has a comma as delimiter. I do:
\COPY products(title, department) from 'toyd.csv' with (DELIMITER ',');
All super cool.
However, title and department are both strings. I have some commas that are in these columns that I don't want to be interpreted as delimiters. So I pass the strings in quotes. But this doesn't work. Postgres still thinks they are delimiters. What are my missing?
Here is a snippet from the CSV that causes the problem:
"Light","Reading, Writing & Spelling"
Any ideas?
You aren't using CSV format there, just a comma-delimited one.
Tell it you want FORMAT CSV and it should default to quoted text - you could also change the quoting character if necessary.