Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load NULL TIMESTAMP with TIME ZONE using COPY FROM in PostgreSQL

Tags:

csv

postgresql

I have a CSV file that I'm trying to load into a PostgreSQL 9.2.4 database using the COPY FROM command. In particular there is a timestamp field that is allowed to be null, however when I load "null values" (actually just "") I get the following error:

ERROR:  invalid input syntax for type timestamp with time zone: ""

An example CSV file looks as follows:

id,name,joined
1,"bob","2013-10-02 15:27:44-05"
2,"jane",""

The SQL looks as follows:

CREATE TABLE "users"
(
    "id" BIGSERIAL NOT NULL PRIMARY KEY,
    "name" VARCHAR(255),
    "joined" TIMESTAMP WITH TIME ZONE,
);

COPY "users" ("id", "name", "joined")
    FROM '/path/to/data.csv'
    WITH (
        ENCODING 'utf-8',
        HEADER 1,
        FORMAT 'csv'
    );

According to the documentation, null values should be represented by an empty string that cannot contain the quote character, which is double quote (") in this case:

NULL

Specifies the string that represents a null value. The default is \N (backslash-N) in text format, and an unquoted empty string in CSV format. You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings. This option is not allowed when using binary format.

Note: When using COPY FROM, any data item that matches this string will be stored as a null value, so you should make sure that you use the same string as you used with COPY TO.

I've tried the option NULL '' but that seems to have no affect. Advice, please!

like image 745
bbengfort Avatar asked Feb 20 '15 04:02

bbengfort


People also ask

How does Postgres store timestamp with timezone?

For timestamp with time zone , the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT ). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone.

How do I get null records in PostgreSQL?

Example - With SELECT Statement Let's look at an example of how to use PostgreSQL IS NULL in a SELECT statement: SELECT * FROM employees WHERE first_number IS NULL; This PostgreSQL IS NULL example will return all records from the employees table where the first_name contains a NULL value.

Is timestamp stored with timezone?

Let's start with some common definitions of the TIMESTAMP and TIMESTAMPTZ data types. The TIMESTAMP data type stores values that include the date and time of day. For example, 12/17/1997 17:37:16. Timestamps are presented without time zone information.

Does Postgres timestamp have timezone?

Introduction to PostgreSQL timestamp The timestamp datatype allows you to store both date and time. However, it does not have any time zone data. It means that when you change the timezone of your database server, the timestamp value stored in the database will not change automatically.


1 Answers

empty string without quotes works normally:

id,name,joined
1,"bob","2013-10-02 15:27:44-05"
2,"jane",

select * from users;
id | name |         joined
----+------+------------------------
 1 | bob  | 2013-10-03 03:27:44+07
 2 | jane |

maybe it would be simpler to replace "" with empty string using sed.

like image 114
alexius Avatar answered Sep 29 '22 07:09

alexius