将数据框复制到具有默认值的列的postgres表 [英] copy dataframe to postgres table with column that has defalut value

查看:112
本文介绍了将数据框复制到具有默认值的列的postgres表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下postgreSql表 stock ,那里的结构与列 insert_time 相同,其默认值 now()

I have the following postgreSql table stock, there the structure is following with column insert_time has a default value now()

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

我正在尝试复制随后的 df

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

我正在使用 psycopg 上传 df 到表 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表将自动

I was expecting with the psycopg copy_from function, my postgresql table will auto-populate the rows along side the insert time.

|  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 =',',column =('id','type','c_date' ,'qty'))

这篇关于将数据框复制到具有默认值的列的postgres表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆