PostgreSQL: how to alter type of a column used by a view or rule

PostgreSQL: how to alter type of a column used by a view or rule

In this article, I will let you know, how to alter type of a column used by view in PostgreSQL.

When you change/alter type of a column for example, you want to change the size of the varchar column and you run alter command and then you get the error message of view is dependent on that column.

A table and a view reflect the same field structure and there is a dependency from view over the table.

You can solve the this problem in single transaction, A transaction can be used to drop the view first, alter the table and then recreate the view.

For example, you have a table users and a view vw_users.

BEGIN;
DROP VIEW vw_users;
ALTER TABLE users ALTER COLUMN column_name TYPE character varying(500);
CREATE VIEW vw_users AS SELECT * FROM users;
COMMIT;

Phone: (+91) 8800417876
Noida, 201301