CREATE RULE

An SQL command for defining a rule

CREATE RULE is a DDL command for defining a rule.

CREATE RULE has always been present in PostgreSQL.

Change history

Examples

Example usage for CREATE RULE, here creating a rule which results in rows with odd-numbered id values being dropped from INSERT operations:

postgres=# CREATE RULE foobar AS
             ON INSERT TO foo
               WHERE NEW.id % 2 = 1
             DO INSTEAD NOTHING;
CREATE RULE

Attempting to insert a sequential series of values results in the target table being populated as follows:

postgres=# INSERT INTO foo VALUES (1), (2), (3);
INSERT 0 1

postgres=# SELECT * FROM foo;
 id 
----
  2
(1 row)

Categories

DDL, Rules, SQL command

See also

ALTER RULE, DROP RULE, pg_rules