ERROR: column <name> is of type timestamp without time zone but expression is of type character varying
ERROR: column <name> is of type timestamp without time zone but expression is of type character varying
我正在读取从 MS SQL db table 导出的 csv 文件并尝试将数据插入 PostgreSQL (Aurora Serverless) db。当我在 PostgreSQL 数据库中创建等效的 table 时,我使用 found online 的列类型映射将日期时间 (MS SQL) 映射到时间戳 (3) (Postgre SQL) 类型。但是,当我尝试将记录插入 Postgres 数据库时,出现错误:
Batch entry 0 INSERT INTO my_table VALUES (\'10000002\', \'2020-11-22 00:00:00\')\nRETURNING * was aborted:
ERROR: column "Date" is of type timestamp without time zone but expression is of type character varying\n
Hint: You will need to rewrite or cast the expression.\n Position: 37 Call getNextException to see other errors in the batch.
我的 MS SQL table 架构:
ID: varchar(20)
Date: datetime
csv 文件包含:
10000002, 2020-11-22 00:00:00
10000003, 2020-11-22 00:00:00
...
PostgreSQL table schema/create 语句:
CREATE table my_table
(
"ID" varchar(20) not null,
"Date" timestamp(3)
);
在错误消息中,提示说我可能必须转换表达式,但我不确定我应该如何做或者这是否是我的最佳解决方案。
我正在使用以下脚本从 csv 文件加载数据并将其推送到 PostgreSQL 数据库:
import boto3
import csv
rds_client = boto3.client('rds-data')
def batch_execute_statement(sql, sql_parameter_sets, transaction_id=None):
parameters = {
'secretArn': db_credentials_secrets_store_arn,
'database': database_name,
'resourceArn': db_cluster_arn,
'sql': sql,
'parameterSets': sql_parameter_sets
}
if transaction_id is not None:
parameters['transactionId'] = transaction_id
response = rds_client.batch_execute_statement(**parameters)
return response
transaction = rds_client.begin_transaction(
secretArn=db_credentials_secrets_store_arn,
resourceArn=db_cluster_arn,
database=database_name)
sql = "INSERT INTO rigs_latest VALUES (:ID, :Date;"
parameter_set = []
with open('data.csv', 'r') as file:
reader = csv.DictReader(file, delimiter=',')
for row in reader:
entry = [
{'name': 'ID', 'value': {'stringValue': row['ID']}},
{'name': 'Date', 'value': {'stringValue': row['Date']}}
]
parameter_set.append(entry)
response = batch_execute_statement(
sql, parameter_set, transaction['transactionId'])
更新:
如果我使用查询编辑器插入一条记录,例如 insert into rigs_latest values (10000002, '2020-11-22 00:00:00');
,它可以正常工作
这表明代码本身有问题
经过一番研究,我发现我们可以为parameter_set
中的每个entry
指定一个typeHint
。根据 documentation typeHint
允许我们指定参数是时间 TIMESTAMP
像这样:
...
entry = [
{'name': 'ID', 'value': {'stringValue': row['ID']}},
{'name': 'Date','typeHint': 'TIMESTAMP', 'value': {'stringValue': row['Date']}}
]
parameter_set.append(entry)
...
我想这有助于解释器弄清楚我们希望 Date
参数的类型是 TIMESTAMP
我正在读取从 MS SQL db table 导出的 csv 文件并尝试将数据插入 PostgreSQL (Aurora Serverless) db。当我在 PostgreSQL 数据库中创建等效的 table 时,我使用 found online 的列类型映射将日期时间 (MS SQL) 映射到时间戳 (3) (Postgre SQL) 类型。但是,当我尝试将记录插入 Postgres 数据库时,出现错误:
Batch entry 0 INSERT INTO my_table VALUES (\'10000002\', \'2020-11-22 00:00:00\')\nRETURNING * was aborted:
ERROR: column "Date" is of type timestamp without time zone but expression is of type character varying\n
Hint: You will need to rewrite or cast the expression.\n Position: 37 Call getNextException to see other errors in the batch.
我的 MS SQL table 架构:
ID: varchar(20)
Date: datetime
csv 文件包含:
10000002, 2020-11-22 00:00:00
10000003, 2020-11-22 00:00:00
...
PostgreSQL table schema/create 语句:
CREATE table my_table
(
"ID" varchar(20) not null,
"Date" timestamp(3)
);
在错误消息中,提示说我可能必须转换表达式,但我不确定我应该如何做或者这是否是我的最佳解决方案。
我正在使用以下脚本从 csv 文件加载数据并将其推送到 PostgreSQL 数据库:
import boto3
import csv
rds_client = boto3.client('rds-data')
def batch_execute_statement(sql, sql_parameter_sets, transaction_id=None):
parameters = {
'secretArn': db_credentials_secrets_store_arn,
'database': database_name,
'resourceArn': db_cluster_arn,
'sql': sql,
'parameterSets': sql_parameter_sets
}
if transaction_id is not None:
parameters['transactionId'] = transaction_id
response = rds_client.batch_execute_statement(**parameters)
return response
transaction = rds_client.begin_transaction(
secretArn=db_credentials_secrets_store_arn,
resourceArn=db_cluster_arn,
database=database_name)
sql = "INSERT INTO rigs_latest VALUES (:ID, :Date;"
parameter_set = []
with open('data.csv', 'r') as file:
reader = csv.DictReader(file, delimiter=',')
for row in reader:
entry = [
{'name': 'ID', 'value': {'stringValue': row['ID']}},
{'name': 'Date', 'value': {'stringValue': row['Date']}}
]
parameter_set.append(entry)
response = batch_execute_statement(
sql, parameter_set, transaction['transactionId'])
更新:
如果我使用查询编辑器插入一条记录,例如 insert into rigs_latest values (10000002, '2020-11-22 00:00:00');
,它可以正常工作
这表明代码本身有问题
经过一番研究,我发现我们可以为parameter_set
中的每个entry
指定一个typeHint
。根据 documentation typeHint
允许我们指定参数是时间 TIMESTAMP
像这样:
...
entry = [
{'name': 'ID', 'value': {'stringValue': row['ID']}},
{'name': 'Date','typeHint': 'TIMESTAMP', 'value': {'stringValue': row['Date']}}
]
parameter_set.append(entry)
...
我想这有助于解释器弄清楚我们希望 Date
参数的类型是 TIMESTAMP