ALTER_VIEW(7pg16)
β¨ ALTER VIEW β bestie's guide to glow-ups for your views β¨
okay so this is the ALTER VIEW page from the PostgreSQL 16.15 documentation, and it's literally the manual for changing the definition of a view π
no cap, it's short, it's sweet, and it's gonna slay.
πΈ SYNOPSIS πΈ
like, here's the whole vibe in one look:
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 π¦
ALTER VIEW changes various auxiliary properties of a view. that's the whole job π«Ά
ok but real talk: if what you actually wanna do is modify the view's defining query, you do NOT use this β use CREATE OR REPLACE VIEW instead. different tool, different job, iykyk.
permissions check, and this is important so like, pay attention bestie π:
- 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 be able to
SET ROLEto the new owning role, and that role must haveCREATEprivilege on the view's schema.
and why are those restrictions there? bc they enforce that altering the owner doesn't do anything you couldn't already do by dropping and recreating the view. the math is mathing β¨
tiny exception, don't come for me: a superuser can alter ownership of any view anyway. main character energy fr.
π PARAMETERS π
nameβ the name (optionally schema-qualified) of an existing view. yes, an existing one, we're not manifesting here.column_nameβ name of an existing column.new_column_nameβ new name for an existing column. she's rebranding πIF EXISTSβ do not throw an error if the view does not exist. a notice is issued in this case. so chill, no drama, 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. she wins, periodt π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, drumroll please π₯: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.security_invoker(boolean) β changes the security-invoker 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 β and this is the catch β the only variants of ALTER TABLE that are allowed with views are equivalent to the ones shown above. so like, don't get creative, it's giving "same options, different font" π
π₯ 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? the second insert goes through the view and gets now(), the first one goes straight to the table and gets NULL. the way that default takes precedenceβ¦ obsessed ππ
β COMPATIBILITY β
ALTER VIEW is a PostgreSQL extension of the SQL standard. so bestie, this is our own special little thing, ngl πΈ
π§ SEE ALSO π§
CREATE VIEW(CREATE_VIEW(7))DROP VIEW(DROP_VIEW(7))
and that's the whole page, bestie π this was PostgreSQL 16.15, dated 2026 β go forth and alter those views, you're gonna eat, no cap π«Άβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-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