ALTER_VIEW(7pg18)
โจ ALTER VIEW โจ
okay bestie, this is the ALTER VIEW page from the PostgreSQL 18.6 manual, and it's literally all about changing the definition of a view. no cap. ๐
๐ธ NAME ๐ธ
ALTER_VIEW โ change the definition of a view
๐ 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_ROLE | 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 [, ... ] )
๐ DESCRIPTION ๐
So ALTER VIEW changes all the auxiliary properties of a view. that's the side-quest stuff, not the main plot. ๐ซถ
heads up: if you actually wanna mess with the view's defining query, that's a whole different vibe โ use CREATE OR REPLACE VIEW instead, bestie. โจ
Now. permission check, because of course there's a permission check. ๐ฅ
- You must own the view to use
ALTER VIEW. no ownership, no slay. - Wanna change a view's schema? you also need
CREATEprivilege on the new schema. both. periodt. - Wanna change the owner? you need to be able to
SET ROLEto the new owning role, and that role needsCREATEprivilege on the view's schema.
the reason for all these restrictions: they make sure that altering the owner doesn't let you do anything you couldn't already do by dropping and recreating the view. so it's givingโฆ security. iconic. ๐
tiny loophole though: a superuser can alter ownership of any view anyway. main character energy fr fr. ๐
๐ PARAMETERS ๐
nameโ the name of an existing view. optionally schema-qualified, live your truth.column_nameโ the name of an existing column.new_column_nameโ the new name for an existing column. glow-up moment โจIF EXISTSโ do not throw an error if the view doesn't exist. instead, a notice gets issued. gentle rejection, not a crash out. ๐ฆSET/DROP DEFAULTโ these forms set or remove the default value for a column. here's the tea: a view column's default value gets substituted into anyINSERTorUPDATEcommand whose target is the view, before applying any rules or triggers for the view. so the view's default will take precedence over any default values from underlying relations. it wins. ๐new_ownerโ the user name of the new owner of the view.new_nameโ the new name for the view.new_schemaโ the new schema for the view.
๐
SET ( view_option_name [= view_option_value] [, ... ] ) / RESET ( view_option_name [, ... ] )
sets or resets a view option. currently supported options are, and yes there are exactly three:
check_option(enum) โ changes the check option of the view. the value must belocalorcascaded. those are the only two. don't try anything else. ๐security_barrier(boolean) โ changes the security-barrier property of the view. the value must be a Boolean value, such astrueorfalse.security_invoker(boolean) โ changes the security-invoker property of the view. the value must be a Boolean value, such astrueorfalse.
โญ NOTES โญ
for historical reasons (iykyk), 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. that's it. that's the whole list. ๐ง
๐ EXAMPLES ๐
wanna rename the view foo to bar? say less:
ALTER VIEW foo RENAME TO bar;
wanna attach a default column value to an updatable view? okay bestie, watch this:
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 the two inserts got different results? that's the whole point. ๐
๐ธ COMPATIBILITY ๐ธ
ALTER VIEW is a PostgreSQL extension of the SQL standard. it's ours, we made it, we're keeping it. ๐
๐ SEE ALSO ๐
CREATE VIEW(CREATE_VIEW(7))DROP VIEW(DROP_VIEW(7))
okay that's the whole page, bestie!! you ate. ๐ โจ
this has been PostgreSQL 18.6, dated 2026 โ mwah, go alter some views, ily ๐ซถ๐
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms