Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot \copy into postgresql table which has default clock_timestamp

Tags:

postgresql

My table has a NOT NULL column called 'created' with DEFAULT CLOCK_TIMESTAMP(). My CSV file from which I am copying intentionally does not have a column called 'created' because I want the database to use the default value for it. My copy command is:

\copy table_name from '/local/path/to/file.csv' delimiter ',' CSV HEADER

The error I receive is:

missing data for column "created_"

The PostgreSQL documentation says: "If there are any columns in the table that are not in the column list, COPY FROM will insert the default values for those columns."

Thanks for any help

like image 778
SplendidSplinter Avatar asked Oct 07 '16 12:10

SplendidSplinter


People also ask

When loading data into a PostgreSQL table what is the default delimiter for data files?

The default is a tab character in text format, a comma in CSV format. This must be a single one-byte character.

How do I get the default date in PostgreSQL?

The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format. In this format, 'YYYY' is a 4-digit year, 'MM' is a 2-digit month, and 'DD' is a 2-digit day. The returned value is a date data type.

How does PostgreSQL default value work?

In a table definition, default values are listed after the column data type. For example: CREATE TABLE products ( product_no integer, name text, price numeric DEFAULT 9.99 ); The default value can be an expression, which will be evaluated whenever the default value is inserted (not when the table is created).

What is the default Ascii quotation character in CSV mode?

Specifies the ASCII quotation character in CSV mode. The default is double-quote.


1 Answers

The clue is in the "that are not in the column list".

You need to specify the "column list" for this to work and leave out the one with the default value:

\copy table_name (column1, column2, ...) from '/local/path/to/file.csv' delimiter ',' CSV HEADER
like image 84
a_horse_with_no_name Avatar answered Dec 20 '22 05:12

a_horse_with_no_name