Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create insert-only table in PostgreSQL

Tags:

postgresql

Is there an SQL statement to create an insert-only table in PostgreSQL (v9.5); in other words, with write permissions it is possible to add rows, but not possible to change existing rows?

CREATE TABLE public.newtable

Tables are only updated with INSERT statements, not using UPDATE or DELETE statements.

like image 314
Joost Döbken Avatar asked Mar 10 '16 14:03

Joost Döbken


Video Answer


1 Answers

This command will allow you to REVOKE any privileges you may have GRANTed:

REVOKE ALL ON TABLE newtable FROM PUBLIC;

Then, you'll want to GRANT the INSERT privilege to the role(s) that need to insert data:

GRANT INSERT ON TABLE newtable TO role;

Despite the above, the database/schema owner can still do whatever he or she would like. So, you should consider creating a specific role for the purpose.

like image 115
titanofold Avatar answered Oct 09 '22 11:10

titanofold