将dataframe复制到具有defalut值的postgres表

2024-10-16 17:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的postgreSql表stock,下面的结构是列insert_time有一个默认值now()

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

我试图copy以下是df

^{pr2}$

我使用psycopgdf上传到表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表将在插入时自动填充行。在

|  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 |

Tags: fromiddfdatetimetypestockcolumn