ALTER_VIEW(7pg14)
β¨ ALTER VIEW β¨
Okay bestie, so this is the ALTER VIEW page from the PostgreSQL 14.24 docs, and it's literally all about tweaking a view after you've already made it. Let's get into it π
πΈ NAME πΈ
ALTER VIEW β change the definition of a view. That's the whole vibe. You're not rewriting the query, you're just messing with the auxiliary stuff around 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_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 "various auxiliary properties" of a view β like the little side details, not the main character herself. And likeβ¦ if you actually wanna modify the view's defining query, that's not this. That's CREATE OR REPLACE VIEW. Different tool, bestie π
Rules of the game, no cap:
- You must own the view to use
ALTER VIEW. - If you're changing a view's schema, you also need
CREATEprivilege on the new schema. - If you're altering the owner, you also need to be a direct or indirect member of the new owning role, and that role must have
CREATEprivilege on the view's schema.
Why these restrictions tho? Because they enforce that altering the owner "doesn't do anything you couldn't do by dropping and recreating the view." Okay that's fair honestly. BUT β 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. Gotta exist (unless you've gotIF EXISTSin your back pocket).column_nameβ name of an existing column.new_column_nameβ the new name for an existing column, slay β¨IF EXISTSβ do not throw an error if the view doesn't exist. Instead a notice gets issued. Very chill of it.SET/DROP DEFAULTβ these forms set or remove the default value for a column. And the details matter here, so listen up: 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 takes precedence over any default values from underlying relations. 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 / RESET view options
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.
π§ 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, it's the same thing with a different name π
π EXAMPLES π
Renaming the view foo to bar, iconic:
ALTER VIEW foo RENAME TO bar;
Attaching a default column value to an updatable view β watch the comments, they're doing a lot of the explaining:
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 view's default really said "I'll take it from here" π«Ά
β COMPATIBILITY β
ALTER VIEW is a PostgreSQL extension of the SQL standard. So if you're coming from somewhere else looking for it β it's a Postgres thing, iykyk.
π SEE ALSO π
CREATE VIEW(CREATE_VIEW(7))DROP VIEW(DROP_VIEW(7))
and that's the whole page, bestie πΈβ¨ This was the PostgreSQL 14.24 docs, dated 2026, ALTER VIEW(7) β go forth and alter responsibly ππ¦π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-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