I am new to postgres and I play around with data loading.
Here is the table definition from postgres 9.2 spec:
CREATE TABLE weather (
city varchar(80),
temp_lo int, -- low temperature
temp_hi int, -- high temperature
prcp real, -- precipitation
date date
);
I prepared the following data file (weather.txt):
San Francisco 43 57 0.0 '1994-11-29'
Hayward 54 37 0.0 '1994-11-29'
and ran the COPY command:
COPY weather FROM '~aviad/postsgres/playground/weather.txt';
now, when I run select * from weather; I see that single quotes appears around the city values.
This does not happen when I run simple INSERT e.g:
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
I wonder:
- what is the reason for wrapping text values by single quotes?
- What is the correct way to put the text data in the file used by
COPYto avoid single quote wrapping?
What you describe in your question is obviously not what’s really happening.
COPYwould fail trying to import string literals with redundant single quotes into adatecolumn.To get rid of redundant quotes, import to a temporary table with
textcolumn, thenINSERT INTOthe target table trimming the quotes:The temp table is dropped automatically at the end of your session.
Reserved words as identifiers
I see you copied the example from the manual. Here is the deep link to the current manual.
While being correct, that example in the manual is unfortunate. I’d advise not to use reserved words like
dateas column names. As you can see heredateis a reserved word in every SQL standard. It’s allowed to use in Postgres and I can see how it’s tempting for a simple example. But that doesn’t make it a good idea. Generally, you should be in the habit of avoiding reserved words as identifiers. It leads to confusing error messages and needlessly incompatible SQL code.