1

In my psql script all foreign key references include the clause on delete cascade on update cascade, because the default is on delete no action on update no action:

create table x (
  y bigint
    references schema1.table1(column1) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE
);

This construct is repeated hundreds of times.

So I would rather like to write:

create table x (
  y bigint 
    references schema1.table1(column1)
);

How to change the default in Postgres temporarily?

1
  • You can also run a plpgsql loop over information_schema that dynamically constructs and executes an adequate alter table..alter column on all non-system objects using a given setting, default or otherwise. Commented Oct 27, 2024 at 13:03

1 Answer 1

2

No, you will have to repeat the text every time.

I understand your desire; I often wish that NOT NULL were the default for columns. But such a feature would be dangerous. Any configuration that alters the semantics of SQL is problematic, because it leads to confusion. Imagine there were such a setting, and you posted your SQL code in — say — a Stackoverflow question: we'd always have to think of clarifying how you configured the database before we could even understand your code.

You don't have to write your table definitions every day. Use a good text editor and define a macro.

Sign up to request clarification or add additional context in comments.

1 Comment

Fair enough answer. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.