ALTER_VIEW(7pg17)
β¨ ALTER VIEW β¨
Okay bestie, so this is the ALTER VIEW page from the PostgreSQL 17.11 Documentation β it's giving "glow-up for your views" energy, and honestly? Iconic. Let's get into it. π
π NAME π
ALTER VIEW β change the definition of a view. That's it, that's the vibe. πΈ
β¨ 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 [, ... ] )
Like, look at all those forms. She's versatile. She contains multitudes. π¦
π DESCRIPTION π
So ALTER VIEW changes various auxiliary properties of a view β the little side details, not the whole thing. And listen, if what you actually wanna do is modify the view's defining query, you don't want this page, you want CREATE OR REPLACE VIEW. Different tool, different job. π₯
Now, the rules, because there are always rules:
- You must own the view to use
ALTER VIEW. No ownership, no entry. π - 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.
The way these restrictions work is honestly so sensible β they enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the view. Like, no sneaky privilege loopholes, bestie. π«Ά
BUT β and this is the plot twist β a superuser can alter ownership of any view anyway. Main character energy, truly. π
π PARAMETERS π
nameβ the name (optionally schema-qualified) of an existing view. Yes, schema-qualified is allowed, live your life.column_nameβ name of an existing column.new_column_nameβ the new name for an existing column. Fresh start, new you. π¦IF EXISTSβ don't throw an error if the view doesn't exist. Instead, a notice is issued. Polite decline energy, love that.SET/DROP DEFAULTβ these forms set or remove the default value for a column. Now 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. Yes, she goes first. 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: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 the only variants of ALTER TABLE that are allowed with views are equivalent to the ones shown above. So like... it works, but it's the same thing. Don't get cute with it. π
π₯ 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, this example ate, no cap:
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
Look at that last line β ts receiving the current time through the view while the base table gets a NULL. The way the view's default takes precedence... obsessed. β
π COMPATIBILITY π
ALTER VIEW is a PostgreSQL extension of the SQL standard. Not standard, still slay. π§
πΈ SEE ALSO πΈ
CREATE VIEW(CREATE_VIEW(7))DROP VIEW(DROP_VIEW(7))
And that's the whole page, bestie β go forth and alter those views responsibly. π
Signing off with love: this was PostgreSQL 17.11, dated 2026, page ALTER VIEW(7). Iykyk. π¦
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-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