Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct COPY command to load postgreSQL data from csv file that has single-quoted data?

I have csv file that has contents like this:

10,53073,0,0,'Y','2008-05-30 21:46:55',0,'2008-05-30 21:48:04',0,53071,2

I want to load the csv data into my_table.

CREATE TABLE my_table
(
  ad_tree_id numeric(10,0) NOT NULL,
  node_id numeric(10,0) NOT NULL,
  ad_client_id numeric(10,0) NOT NULL,
  ad_org_id numeric(10,0) NOT NULL,
  isactive character(1) NOT NULL DEFAULT 'Y'::bpchar,
  created timestamp without time zone NOT NULL DEFAULT now(),
  createdby numeric(10,0) NOT NULL,
  updated timestamp without time zone NOT NULL DEFAULT now(),
  updatedby numeric(10,0) NOT NULL,
  parent_id numeric(10,0),
  seqno numeric(10,0),
  CONSTRAINT ad_treenodemm_pkey PRIMARY KEY (ad_tree_id , node_id ),
  CONSTRAINT adtree_adtreenodemm FOREIGN KEY (ad_tree_id)
      REFERENCES ad_tree (ad_tree_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  CONSTRAINT ad_treenodemm_isactive_check CHECK (isactive = ANY (ARRAY['Y'::bpchar, 'N'::bpchar]))
)

When I run this command in pgAdmin III tool:

COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV;

I got error:

ERROR:  value too long for type character(1)
CONTEXT:  COPY my_table, line 1, column isactive: "'Y'"

Then I modified the command like this:

COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE ''';
COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE '\'';
COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE '\'' ESCAPE '\';
COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE '\'' ESCAPE \;

All failed when tried.

So, anyone can show me the correct COPY command for this case?

like image 281
null Avatar asked Oct 12 '12 09:10

null


People also ask

How do I COPY a CSV file into a Postgres table?

After creating the sample CSV file and table, you can now easily execute the PostgreSQL Import CSV task via any of the following methods: Method 1: Perform PostgreSQL Import CSV Job using the COPY Command. Method 2: Perform PostgreSQL Import CSV Job using PgAdmin.

What does the Copy command in PostgreSQL do?

Description. COPY moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already).

How do I use Copy command in PostgreSQL?

PSQL \Copy Command for Client-Side ExportTo copy the entire table to a csv file, use \copy. This will copy the contents of a table to the client computer as a csv file. The file will not contain the headers of the table. \copy employees to '/var/lib/postgresql/emp.


2 Answers

Double single quotes (if standard_conforming_strings is on, see the docs)

COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE '''';

or use the non-standard PostgreSQL-specific escape string:

COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE E'\'';
like image 59
Craig Ringer Avatar answered Sep 20 '22 19:09

Craig Ringer


Some other people who are experiencing this error may want to check the file to see if it contains a header on the first line. While it wasn't the problem in your case, it is worth noting the way to work around it:

COPY my_table FROM 'c:\downloads\file.csv' WITH DELIMITER ',' CSV HEADER;
like image 43
Rupasa Sushma Avatar answered Sep 21 '22 19:09

Rupasa Sushma