Postgres 9.5 (released since 2016-01-07) offers an “upsert” command, also known as an ON CONFLICT clause to INSERT:
INSERT ... ON CONFLICT DO NOTHING/UPDATE
It solves many of the subtle problems you can run into when using concurrent operation, which some other answers propose.
More Related Contents:
- Insert, on duplicate update in PostgreSQL?
- How to UPSERT (MERGE, INSERT … ON DUPLICATE UPDATE) in PostgreSQL?
- Use multiple conflict_target in ON CONFLICT clause
- serial in postgres is being increased even though I added on conflict do nothing
- How to find out if an upsert was an update with PostgreSQL 9.5+ UPSERT?
- Differentiate inserted and updated rows in UPSERT using system columns
- Can I use return value of INSERT…RETURNING in another INSERT?
- Generate DEFAULT values in a CTE UPSERT using PostgreSQL 9.3
- PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values
- How do you use variables in a simple PostgreSQL script?
- How to make “case-insensitive” query in Postgresql?
- Rownum in postgresql
- How to switch databases in psql?
- Postgresql: Scripting psql execution with password
- dynamic sql query in postgres
- Is there a timeout for idle PostgreSQL connections?
- How to make connection to Postgres via Node.js
- docker postgres pgadmin local connection
- Does a Postgres UNIQUE constraint imply an index?
- SELECT raises exception in PL/pgSQL function
- Heroku “psql: FATAL: remaining connection slots are reserved for non-replication superuser connections”
- Changing primary key int type to serial
- Return SETOF rows from PostgreSQL function
- Dynamically generate columns for crosstab in PostgreSQL
- PSQLException: current transaction is aborted, commands ignored until end of transaction block
- PostgreSQL via SSH Tunnel
- How should I import data from CSV into a Postgres table using pgAdmin 3?
- PostgreSQL – set a default cell value according to another cell value
- How to include excluded rows in RETURNING from INSERT … ON CONFLICT
- How to escape question mark (?) character with Spring JpaRepository