LateCoder
LateCoder

Reputation: 2283

Alter a column name in a view in PostgreSQL

I have a table, x, and a view, x_view, that is based on x, in a PostgreSQL database.

x:

CREATE TABLE x (
A double precision
);

x_view:

CREATE VIEW x_view AS
 SELECT x.A;

I renamed a column in x from A to B.

ALTER TABLE x RENAME COLUMN A to B

When I then look at the definition of the view, I see the following:

SELECT x.B AS A from x;

When I do a "SELECT * FROM x_view", the column that should now be labeled B is still labeled A.

Why does the view not automatically change the name of the column when I execute "SELECT * FROM x_view"? Is there any way to force it to do so, besides having to drop and recreate the view?

Upvotes: 5

Views: 15881

Answers (1)

Cao Minh Tu
Cao Minh Tu

Reputation: 603

You can use ALTER TABLE x_view RENAME COLUMN A to B to rename your view column.

Source: @Peter Eisentraut's answer here and I checked on my database.

Upvotes: 13

Related Questions