ALTER_VIEW(7pg13)

πŸ“¦ version 13.23, in opensuse-leap 16.0

✨ ALTER VIEW ✨

okay so bestie, welcome to ALTER VIEW β€” the PostgreSQL 13.23 glow-up manual πŸ’… this one's from the 7 section, and we're gonna make your views look different (just the outer vibes though, not the soul β€” more on that in a sec πŸ‘€)


🌸 NAME 🌸

ALTER VIEW β€” change the definition of a view, but like, the auxiliary parts of it πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name SET DEFAULT expression
ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name DROP DEFAULT
ALTER VIEW [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
ALTER VIEW [ IF EXISTS ] name RENAME [ COLUMN ] column_name TO new_column_name
ALTER VIEW [ IF EXISTS ] name RENAME TO new_name
ALTER VIEW [ IF EXISTS ] name SET SCHEMA new_schema
ALTER VIEW [ IF EXISTS ] name SET ( view_option_name [= view_option_value] [, ... ] )
ALTER VIEW [ IF EXISTS ] name RESET ( view_option_name [, ... ] )

like, seven whole forms. she's versatile. main character energy fr fr πŸ‘‘


πŸ’– DESCRIPTION πŸ’–

ALTER VIEW changes various auxiliary properties of a view. That's the key word β€” auxiliary. It's the little side-details, not the actual query.

not me needing to say this but: if you want to modify the view's defining query itself, bestie, that's CREATE OR REPLACE VIEW β€” different tool, different vibe πŸ’…

Okay, the rules (yes there are rules, there are always rules):

  • You must own the view to use ALTER VIEW.
  • To change a view's schema, you must also have CREATE privilege on the new schema.
  • To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the view's schema.

Why so strict? (These restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the view.) like, it's just keeping everything legit, no sneaky shortcuts 🫢

But also β€” a superuser can alter ownership of any view anyway. slay. superusers literally get away with everything 😭


πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name (optionally schema-qualified) of an existing view. yep, that's the one you're messing with 🌸
  • column_name β€” name of an existing column.
  • new_column_name β€” the new name for an existing column. glow-up for one column only ✨
  • IF EXISTS β€” do not throw an error if the view does not exist. a notice is issued in this case. so no dramatic crash, just a lil heads-up πŸ¦‹
  • SET/DROP DEFAULT β€” these forms set or remove the default value for a column. and here's the tea: a view column's default value is substituted into any INSERT or UPDATE command whose target is the view, before applying any rules or triggers for the view. so the view's default will therefore take precedence over any default values from underlying relations. the view is literally the main character here β€” her default wins πŸ‘‘
  • new_owner β€” the user name of the new owner of the view.
  • new_name β€” the new name for the view. rebrand era πŸ’–
  • new_schema β€” the new schema for the view. moving her to a different neighborhood 🏑
  • SET ( view_option_name [= view_option_value] [, ... ] ) and RESET ( view_option_name [, ... ] ) β€” sets or resets a view option. currently supported options are:
    • check_option (enum) β€” changes the check option of the view. the value must be local or cascaded.
    • security_barrier (boolean) β€” changes the security-barrier property of the view. the value must be a Boolean value, such as true or false.

πŸ”₯ NOTES πŸ”₯

for historical reasons, ALTER TABLE can be used with views too πŸ‘€ but the only variants of ALTER TABLE that are allowed with views are equivalent to the ones shown above. so like, don't get creative with it β€” the door's only open that far πŸšͺ


🌈 EXAMPLES 🌈

To rename the view foo to bar:

ALTER VIEW foo RENAME TO bar;

To attach a default column value to an updatable view:

CREATE TABLE base_table (id int, ts timestamptz);
CREATE VIEW a_view AS SELECT * FROM base_table;
ALTER VIEW a_view ALTER COLUMN ts SET DEFAULT now();
INSERT INTO base_table(id) VALUES(1);  -- ts will receive a NULL
INSERT INTO a_view(id) VALUES(2);  -- ts will receive the current time

see how that works?? insert into the view and ts gets the current time, insert into the table and it's just NULL. the view's default is doing the heavy lifting πŸ”₯ (comments are verbatim, i'm not touching them, they're iconic)


🧚 COMPATIBILITY 🧚

ALTER VIEW is a PostgreSQL extension of the SQL standard. so like, standard? never heard of her πŸ’… pg original only.


⭐ SEE ALSO ⭐


okay that's the whole page, no cap 🫢 go forth and alter responsibly, bestie β€” and remember, you gotta own the view before you can restyle it πŸ’œ

πŸ“ PostgreSQL 13.23 Β· 2025 Β· ALTER VIEW(7) πŸ“

πŸ“¦ where this page comes from