import psycopg2
from psycopg2 import extras as ex
conn = psycopg2.connect(database="test", user="postgres", password="db12345678", host="localhost", port="5432")
cursor = conn.cursor()
# values 后面直接%s
sql = '''INSERT INTO "public"."user"("id", "name") values %s'''
datalist = []
# 这里做一个循环, 循环加入数据
# 行数据是以元组的形式存放
datalist.append((3, '1zhangsan'))
datalist.append((2, '2lisi'))
ex.execute_values(cursor, sql, datalist, page_size=1000)
conn.commit() #需要手动提交