ALTER_VIEW(7pg13)
β¨ 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
CREATEprivilege 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
CREATEprivilege 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 anyINSERTorUPDATEcommand 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] [, ... ] )andRESET ( 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 belocalorcascaded.security_barrier(boolean) β changes the security-barrier property of the view. the value must be a Boolean value, such astrueorfalse.
π₯ 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 β
CREATE VIEW(CREATE_VIEW(7))DROP VIEW(DROP_VIEW(7))
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
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms