PostgreSQL UPSERT (in Python)
PostgreSQL does not yet support the UPSERT command (though it is on their Todo list). If you have a row you want to update (if it already exists in the database) or insert (if it doesn’t exist yet), then PostgreSQL unfortunately makes you implement the logic yourself. Other popular databases like SQLite (INSERT OR IGNORE) and MySQL (ON DUPLICATE KEY UPDATE) both support upserting. I haven’t run across a generic PL/pgSQL function which can do this, but you could write a trigger (like this one) for each table where this functionality is needed.
Unfortunately, this is a bit of a pain if you want to use UPSERT on many tables, so I wrote a Python method which takes care of the UPSERT logic generically. To use it, you call it with a cursor connected to your database, the schema and table name, a list of primary key field names, and the key-value pairs for each field.
For example, let’s say you have a table which tracks scores (and only the last score counts):
CREATE TABLE MySchema.Scores ( user_id integer PRIMARY KEY, score integer NOT NULL );
To UPSERT a row into this table you would:
db_conn = psycopg2.connect("...") db_cur = db_conn.cursor() upsert(db_cur, 'Scores', ('user_id',), schema='MySchema', user_id=..., score=...) db_conn.commit()
Here’s the code for the Python-based upsert method: