Question

Is there a way to create an immutable field/cell in a Postgres record? AKA, only allow the field to be written once, perhaps just on insert?

Use case: I have a "uuid" field for a record, and it should never ever be changed (same with the PK as well).

Était-ce utile?

La solution

There are several techniques for that:

  • Using column permissions, grant UPDATE only on the other columns.

  • Create an UPDATE trigger that throws an exception when OLD.id <> NEW.id.

Note that all these measures can never deny the table owner access, since the owner can always change permissionsand drop triggers.

Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top