Python 如何使用psycopg将时间戳字符串插入postgres db?

Python 如何使用psycopg将时间戳字符串插入postgres db?,python,sql,postgresql,sql-insert,psycopg2,Python,Sql,Postgresql,Sql Insert,Psycopg2,我有一张桌子: CREATE TABLE TABLE_WITH_A_TIMESTAMP ( TIMESTAMP_ID varchar(4), TIMESTAMP timestamp ) 以及与其他工具配合使用的INSERT命令: 插入“my_schema”。“带有时间戳(时间戳id,时间戳)值的表_('0001',{ts'2020-01-01 00:00:00.001') 现在我想使用psycopg2在我的表上执行这个命令 query = """INS

我有一张桌子:

CREATE TABLE TABLE_WITH_A_TIMESTAMP
(
   TIMESTAMP_ID varchar(4),
   TIMESTAMP timestamp
)
以及与其他工具配合使用的
INSERT
命令:
插入“my_schema”。“带有时间戳(时间戳id,时间戳)值的表_('0001',{ts'2020-01-01 00:00:00.001')

现在我想使用psycopg2在我的表上执行这个命令

query = """INSERT INTO "my_schema"."table_with_a_timestamp" (timestamp_id, timestamp) VALUES ('0001', {ts '2020-01-01 00:00:00.001'});"""
cursor.execute(query)
这不起作用,错误消息为:

syntax error at or near "{"
LINE 1: ...rsion) VALUES ('0001',{ts '2020-...
                                                             ^
: ProgrammingError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 35, in lambda_handler
    cursor.execute(query)
psycopg2.ProgrammingError: syntax error at or near "{"
LINE 1: ...rsion) VALUES ('0001',{ts '2020-...
将时间戳作为python对象处理也有类似的问题。但是,我不希望解析该命令,而是转换为python时间戳,然后执行字符串插值。因为我是从一个文件中读取命令,所以我更倾向于使用正确的格式(不一定是python格式)读取命令。 上面的字符串命令的正确格式是什么,以便将时间戳写入表

我试过:
“…,'ts{2020-01-01 00:00:00.001},…”
->类型时间戳的输入语法无效
“…”,“{ts'2020-01-01 00:00:00.001'”,“…”
->列“{ts'2001-08-13 10:19:47.701'}”不存在

“…,ts'2020-01-01 00:00:00.001',…”
->类型“ts”不存在

{ts'2020-01-01 00:00:00.001')
特定于其他工具。Postgres正确的语法是

INSERT INTO "my_schema"."table_with_a_timestamp" (timestamp_id, timestamp) 
VALUES ('0001', timestamp '2020-01-01 00:00:00.001');
尽管在这种情况下不需要使用单词
时间戳
,请参阅