ALTER_VIEW(7pg14)

πŸ“¦ version 14.24, in opensuse-leap 16.0

✨ 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 CREATE privilege 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 CREATE privilege 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 got IF EXISTS in 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 any INSERT or UPDATE command 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 be local or cascaded.
  • security_barrier (boolean) β€” changes the security-barrier property of the view. The value must be a Boolean value, such as true or false.

🧚 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 πŸ’œ


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