Example 1: return insert results in POSTGRESQL
INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING id, firstname;
INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING *;
Example 2: insert postgres
INSERT INTO films (code, title, did, date_prod, kind)
VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
Example 3: sql insert query
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Example 4: postgresql insert select
insert into items_ver(item_id, item_group, name)
select * from items where item_id=2;
Example 5: posgres insert
INSERT INTO films
(code, title, did, date_prod, kind)
VALUES
('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
Example 6: insert record into table postgresql
try:
connection = psycopg2.connect(**postgres_credentials())
cursor = connection.cursor()
order_types = [(1,'SELL'),(2,'BUY')]
placeholders = ','.join(['%s']*len(order_types))
sql = f"""
INSERT INTO collection.instruction(id, side)
VALUES {placeholders}
"""
insert_statement = cursor.mogrify(sql, order_types)
print(insert_statement.decode('utf-8'))
cursor.execute(insert_statement)
connection.commit()
print('DB entries committed.')
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if connection:
cursor.close()
connection.close()