Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a tab in a postgres front-end COPY

Tags:

csv

postgresql

I would like to use the psql "\copy" command to pull data from a tab-delimited file into Postgres. I'm using this command:

\copy cm_state from 'state.data' with delimiter '\t' null as ; 

But I'm getting this warning (the table actually loads fine):

WARNING:  nonstandard use of escape in a string literal LINE 1: COPY cm_state FROM STDIN DELIMITER '\t' NULL AS ';' HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'. 

How do I specify a tab if '\t' is not correct?

like image 506
Chris Curvey Avatar asked May 24 '11 15:05

Chris Curvey


People also ask

What is the use of delimiter in PostgreSQL?

The delimiter is a string used as the delimiter for splitting. The position argument sets the part of the string that is to be returned, starting from 1. The argument must have a positive integer as its value.

What is PSQL's \copy?

Psql \copy command is used when you want to export the data from Postgres table to a CSV file on a client machine. To use this command, you will need access to the psql prompt. You will understand it more with the following psql copy examples. To copy the entire table to a csv file, use \copy.

How do I COPY a table in PostgreSQL?

To copy a table with partial data from an existing table, users can use the following statement: Syntax: CREATE TABLE new_table AS SELECT * FROM existing_table WHERE condition; The condition in the WHERE clause of the query defines which rows of the existing table will be copied to the new table.

How do I COPY a column name in PostgreSQL?

You can provide the columns your want to fill with the COPY command. Like so: \copy your_table (x2,x5,x7,x10) FROM '/path/to/your-file.


2 Answers

Use E'\t' to tell postgresql there may be escaped characters in there:

\copy cm_state from 'state.data' with delimiter E'\t' null as ';' 
like image 194
Seth Robertson Avatar answered Sep 22 '22 04:09

Seth Robertson


you can do this copy cm_state from stdin with (format 'text')

like image 32
user4372693 Avatar answered Sep 20 '22 04:09

user4372693