Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql, Restrict update on specific columns (Read Only)

Tags:

postgresql

Is it possible to have postresql restrict/prevent an update on a specific record if the update includes changes to specific columns?

How would this be implemented. A trigger/constraint? What would be the most efficient way to implement this?

I am using version 9.1

like image 384
stellard Avatar asked Mar 14 '13 14:03

stellard


People also ask

How do you update a specific column in PostgreSQL?

First, specify the name of the table that you want to update data after the UPDATE keyword. Second, specify columns and their new values after SET keyword. The columns that do not appear in the SET clause retain their original values. Third, determine which rows to update in the condition of the WHERE clause.

Do Postgres views update automatically?

Updatable Views. Simple views are automatically updatable: the system will allow INSERT , UPDATE and DELETE statements to be used on the view in the same way as on a regular table.

What is select for update Postgres?

The SELECT FOR UPDATE statement is used to order transactions by controlling concurrent access to one or more rows of a table. It works by locking the rows returned by a selection query, such that other transactions trying to access those rows are forced to wait for the transaction that locked the rows to finish.


2 Answers

The easiest way is to create BEFORE UPDATE trigger that will compare OLD and NEW row and RAISE EXCEPTION if the change to the row is forbidden.

like image 189
Ihor Romanchenko Avatar answered Sep 18 '22 18:09

Ihor Romanchenko


No, but it should be pretty trivial to write. Just set up a BEFORE UPDATE trigger that compares old field against new field and does a RAISE ERROR if they're different. The pgSQL docs have a few examples of how to write a trigger function.

like image 32
GoT Avatar answered Sep 18 '22 18:09

GoT