I have an SQL file which is manually generated. I use the code below to create it.
conn = psycopg2.connect(host=t_host, port=t_port, dbname=t_dbname, user=t_name_user, password=t_password)
cursor = conn.cursor()
table_name='product'
with open("table_dump.sql","w+") as f:
create_query ="""CREATE TABLE public.decima
(
product_name character varying(200) COLLATE pg_catalog."default",
product_owner character varying(20) COLLATE pg_catalog."default",
trigger_operation character varying(4) COLLATE pg_catalog."default",
name character varying(250) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT product_pkey PRIMARY KEY (name)
)
TABLESPACE pg_default;"""
f.write('%s \n' % (create_query))
#select table query
cursor.execute("SELECT * FROM %s" % (table_name))
column_names = []
columns_descr = cursor.description
for c in columns_descr:
column_names.append(c[0])
#insert query (insert data)
insert_prefix = 'INSERT INTO %s (%s) VALUES ' % (table_name, ', '.join(column_names))
rows = cursor.fetchall()
for row in rows:
row_data = []
for rd in row:
if rd is None:
row_data.append('NULL')
elif isinstance(rd, datetime.datetime):
row_data.append("'%s'" % (rd.strftime('%Y-%m-%d %H:%M:%S') ))
else:
row_data.append(repr(rd))
f.write('%s (%s);\n' % (insert_prefix, ', '.join(row_data)))
When I used "table_dump.sql" to restore db, the exit code appeared. But I executed this file as a query, then my database still be created and work as usual.
So how can to fix this problem. Thank you.
This is the way I tried to restore
This is the detail of error


%up earlier. If you're using placeholders, note, no quotes around it are required. They may wreck your query.