使用具有默认值的列将数据框复制到 postgres table

copy dataframe to postgres table with column that has defalut value

我有以下 postgreSql table stock,结构如下,列 insert_time 具有默认值 now()

|    column   |  pk |    type   |
+-------------+-----+-----------+
| id          | yes | int       |
| type        | yes | enum      |
| c_date      |     | date      |
| qty         |     | int       |
| insert_time |     | timestamp |

我正在尝试 copy 以下 df

|  id | type |    date    | qty  |
+-----+------+------------+------+
| 001 | CB04 | 2015-01-01 |  700 |
| 155 | AB01 | 2015-01-01 |  500 |
| 300 | AB01 | 2015-01-01 | 1500 |

我正在使用 psycopgdf 上传到 table stock

cur.copy_from(df, stock, null='', sep=',')
conn.commit()

收到此错误。

DataError: missing data for column "insert_time"
CONTEXT:  COPY stock, line 1: "001,CB04,2015-01-01,700"

我期待 psycopg copy_from 函数,我的 postgresql table 将在插入时间旁边自动填充行。

|  id | type |    date    | qty  |     insert_time     |
+-----+------+------------+------+---------------------+
| 001 | CB04 | 2015-01-01 |  700 | 2018-07-25 12:00:00 |
| 155 | AB01 | 2015-01-01 |  500 | 2018-07-25 12:00:00 |
| 300 | AB01 | 2015-01-01 | 1500 | 2018-07-25 12:00:00 |

您可以像这样指定列:

cur.copy_from(df, stock, null='', sep=',', columns=('id', 'type', 'c_date', 'qty'))