ALTER_VIEW(7pg17)

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

✨ 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 CREATE privilege on the new schema.
  • To alter the owner, you must be able to SET ROLE to the new owning role, and that role must have CREATE privilege 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 any INSERT or UPDATE command 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 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.
    • security_invoker (boolean) β€” changes the security-invoker 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... 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 🌸


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