django中动态新建postgres数据库表
阅读原文时间:2023年07月08日阅读:2

import psycopg2

def create_new_table(table_id):
conn = psycopg2.connect(database='Test', user='postgres', password='123456', host='127.0.0.1', port='5432')
cur = conn.cursor()
print('aaaaa')
cur.execute('''CREATE TABLE DataFile%s
(nid INT PRIMARY KEY NOT NULL,
create_time DATE NOT NULL,
message_head TEXT,
message_body_data bytea,
message_body_param TEXT);''' % table_id)

print("Table created successfully")  

conn.commit()  
conn.close()