ossc-db / pg_bulkload

High speed data loading utility for PostgreSQL
http://ossc-db.github.io/pg_bulkload/index.html
Other
443 stars 75 forks source link

new lines in fields #128

Closed mybigman closed 2 years ago

mybigman commented 2 years ago

Is there a way to support new line characters in fields?

col,"col
blah
blah",col,col

I saw issue #36 which looks like it was removed then added back from the way I read that?

mikecaat commented 2 years ago

Yes, I think so. #36 said revering 59cda299e2706a6231c3bf8041208c5abc05f648 to continue supporting new line characters. I tested in my environment and it works.

$ cat init.sql 
DROP EXTENSION IF EXISTS pg_bulkload;
DROP TABLE IF EXISTS pg_bulkload_test;

CREATE EXTENSION pg_bulkload;

CREATE TABLE pg_bulkload_test(
        id int,
        name varchar,
        age int,
        PRIMARY KEY(id)
);

# The second record has new line chracters
$ cat load.csv 
1,bob,13
2,"ma
ry
ry",24
3,yama,42
4,kan,33
5,john,55

$ cat sample_csv.ctl 
OUTPUT = pg_bulkload_test                   # [<schema_name>.]table_name
INPUT = /tmp/load.csv  # Input data location (absolute path)
TYPE = CSV                            # Input file type
QUOTE = "\""                          # Quoting character
ESCAPE = \                            # Escape character for Quoting
DELIMITER = ","                       # Delimiter
LOGFILE = /tmp/pg_bulkload.log

$ cat pg_bulkload.sh
#!/bin/bash

BASE=`dirname $0`

cp ${BASE}/load.csv /tmp/load.csv
pg_bulkload ${BASE}/sample_csv.ctl

$ psql -f init.sql 

$ bash pg_bulkload.sh
NOTICE: BULK LOAD START
NOTICE: BULK LOAD END
        0 Rows skipped.
        5 Rows successfully loaded.
        0 Rows not loaded due to parse errors.
        0 Rows not loaded due to duplicate errors.
        0 Rows replaced with new rows.

$ psql -c "SELECT * FROM pg_bulkload_test;"  
 id | name | age
----+------+-----
  1 | bob  |  13
  2 | ma  +|  24
    | ry  +|
    | ry   |
  3 | yama |  42
  4 | kan  |  33
  5 | john |  55
(5 rows)
mybigman commented 2 years ago

@mikecaat thanks again mate... was missing the "escape" option.