首页 > 解决方案 > 如何使用 now () 和 \copy from 命令更新我的 postgres 数据库中名为 created_on 的列的当前时间

问题描述

我正在将csv文件从本地加载psqlpostgres使用\copy. 问题是,created_on每当记录更新但 created_on 具有没有时区类型且具有非空约束的时间戳时,就有一个称为它需要更新到系统时间的列。那么如何在一个语句中加载csv文件并同时将其设置为 now 呢?created_on

\copy counter_template(counter_name,category,counter_type_name,module_name,default_instance_name,query_string,execution_type,user_id,enterprise_id,created_by,display_name,unit) from 'F:\Work\test2.csv' with delimiter as ',' FORCE NOT NULL default_instance_name,query_string,execution_type HEADER csv;

错误:

ERROR:  null value in column "created_on" violates not-null constraint
DETAIL:  Failing row contains (5470, hostname, status, null, Apache_Metricset, null, Application, f, f, null, f, f, 1, , f, , , f, 1, 0, f, null, null, 1, null, Hostname, keyword, f, f, f, 0, , f, null, f, null).

标签: postgresql

解决方案


您需要一个 在表上提供当前时间的BEFORE INSERT 触发器created_on


推荐阅读