ALTER TABLE g_stations ADD COLUMN altitute_tmp TYPE int;
COPY g_stations (id, longitude, latitude, name, altitute_tmp) FROM '/usr/local/pgsql/share/g_stations.csv' WITH DELIMITER AS ' ' NULL AS '' CSV HEADER;
UPDATE g_stations SET COLUMN altitute = COALESCE(altitute_tmp,0);
ALTER TABLE g_stations DROP COLUMN altitute_tmp;
The problem is with the NULL AS ‘’ which doesn’t work because there is not an empty string in the csv file, but just plain nothing for that column. And may be to make it even a bit worse, it’s the last column. May be that’s no difference, though.
Regards, Robert
swingman:
What about running
ALTER TABLE g_stations ADD COLUMN altitute_tmp TYPE int;
COPY g_stations (id, longitude, latitude, name, altitute_tmp) FROM ‘/usr/local/pgsql/share/g_stations.csv’ WITH DELIMITER AS ’ ’ NULL AS ‘’ CSV HEADER;
UPDATE g_stations SET COLUMN altitute = COALESCE(altitute_tmp,0);
ALTER TABLE g_stations DROP COLUMN altitute_tmp;
I assume you mean by real an empty string like “” or ‘’?
Anyway, it would not help as I get this format from the swiss railway in the current form and have to find a way to use it as it is. Actually we import it for many years already, so that’s not a problem.
The reason I am experimenting with COPY FROM is that we are starting to use PostgreSQL WITH PostGIS, and I have to convert “traditional” data into spatial enabled table data and therefor, COPY FROM is quite handy (or let me say would be if it could handle “real” empty columns in a csv file. That’s why I not yet give up to find a solution for the import of NULL columns in a csv file (and, of course, hope to find a solution). It seems to me quite a normal real world problem, so don’t know why it does not work.
ROCLASI:
Hi Robert,
Perhaps the issue is with the delimiter. Can you export to a real CSV format ?
What about replacing COMMA-RETURN by COMMA-something-RETURN in the text file?
Anyway I always used TAB delimited, CSV is more difficult to deal with, all those quotes…
swingman:
What about replacing COMMA-RETURN by COMMA-something-RETURN in the text file?
Anyway I always used TAB delimited, CSV is more difficult to deal with, all those quotes…