Search code examples
postgresqlpgadminpgadmin-4

PgAdmin Exporting text column to csv file


I have a table with 3 columns - type, name, and code. The code column contains the procedure/function source code. I have exported it to a csv file using Import/Export option in PgAdmin 4 v5, but the code column does not stick to a single cell in the csv file. The data in it spreads across to many of the rows and columns. I have checked Encoding as UTF8 which works fine normally while exporting other tables.

Other settings: Format: csv, Encoding: UTF8. Have not changed any other settings

Can someone help how to export it properly.


Solution

  • An explanation of what you are seeing:

    CREATE TABLE public.csv_test (
        fld_1 character varying,
        fld_2 character varying,
        fld_3 character varying,
        fld_4 character varying
    );
    
    insert into csv_test values ('1', E'line with line end. \n  New line', 'test', 'dog');
    insert into csv_test values ('2', E'line with line end. \n  New line', 'test', 'dog');
    insert into csv_test values ('3', E'line with line end. \n  New line \n Another line', 'test2', 'cat');
    insert into csv_test values ('4', E'line with line end. \n  New line \n \t Another line', 'test3', 'cat');
    
    select * from csv_test ;
     fld_1 |         fld_2         | fld_3 | fld_4 
    -------+-----------------------+-------+-------
     1     | line with line end.  +| test  | dog
           |   New line            |       | 
     2     | line with line end.  +| test  | dog
           |   New line            |       | 
     3     | line with line end.  +| test2 | cat
           |   New line           +|       | 
           |  Another line         |       | 
     4     | line with line end.  +| test3 | cat
           |   New line           +|       | 
           |          Another line |       | 
    
    \copy csv_test to csv_test.csv with (format 'csv');
    \copy csv_test to csv_test.txt;
    
    --fld_2 has line ends and/or tabs so in CSV the data will wrap inside the quotes.
    cat csv_test.csv 
    1,"line with line end. 
      New line",test,dog
    2,"line with line end. 
      New line",test,dog
    3,"line with line end. 
      New line 
     Another line",test2,cat
    4,"line with line end. 
      New line 
             Another line",test3,cat
    
    -- In text format the line ends and tabs are shown and not wrapped.
    cat csv_test.txt 
    1       line with line end. \n  New line        test    dog
    2       line with line end. \n  New line        test    dog
    3       line with line end. \n  New line \n Another line        test2   cat
    4       line with line end. \n  New line \n \t Another line     test3   cat