我试图在我的postgres表中插入它的时间戳,但是每次它关于日期问题的抛出错误。谁能建议我是否要以正确的格式插入日期。让我知道是否还有其他方法可以在postgres列中插入日期。我的server_time列数据类型是没有时区的时间戳
import datetime
import psycopg2
def Main():
data2 = dict()
try:
conn = psycopg2.connect(database="name_db",user="postgres",password="password",host="localhost",port="5432")
cursor = conn.cursor()
print("Connecting to Database")
while True:
try:
server_time = datetime.datetime.utcnow()
server_time = server_time.replace(tzinfo=pytz.utc)
print(server_time)
data2 = {"server_time" : "msg"}
sql = """INSERT INTO name_test(server_time, posts) VALUES (%(server_time), %(msg)s);"""
cursor.executemany(sql, data2)
#cursor.commit
cursor.close
conn.close
我的错误是
Error: 'datetime.datetime' object is not subscriptable