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).

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top