Cuando está insertando una fila (PostgreSQL> = 9.5), y desea que el posible INSERT sea exactamente igual a la posible ACTUALIZACIÓN, puede escribirlo así:
INSERT INTO tablename (id, username, password, level, email)
VALUES (1, 'John', 'qwerty', 5, '[email protected]')
ON CONFLICT (id) DO UPDATE SET
id=EXCLUDED.id, username=EXCLUDED.username,
password=EXCLUDED.password, level=EXCLUDED.level,email=EXCLUDED.email
¿Hay un camino más corto? Solo para decir: use todos los valores EXCLUDE.
En SQLite solía hacer:
INSERT OR REPLACE INTO tablename (id, user, password, level, email)
VALUES (1, 'John', 'qwerty', 5, '[email protected]')
postgresql
upsert
postgresql-9.5
Sebastian
fuente
fuente

INSERT INTO tablename (id, username, password, level, email) VALUES (1, 'John', 'qwerty', 5, '[email protected]') ON CONFLICT (id) DO UPDATE SET (username, password, level, email) = (EXCLUDED.username, EXCLUDED.password, EXCLUDED.level, EXCLUDED.email).casi lo mismo, pero fácil de copiar / pegar / administrar la lista de columnasRespuestas:
Postgres no ha implementado un equivalente a
INSERT OR REPLACE. De losON CONFLICTdocumentos (énfasis mío):Aunque no le da una abreviatura para el reemplazo, se
ON CONFLICT DO UPDATEaplica de manera más general, ya que le permite establecer nuevos valores basados en datos preexistentes. Por ejemplo:fuente