PostgreSQL – Managing Views
Last Updated :
22 Aug, 2024
Views in PostgreSQL are a powerful tool that allows you to simplify complex queries by abstracting them into virtual tables. They are derived from base tables or previously defined views, offering a convenient way to organize and manage your data without creating physical copies.
In this article, we will learn about views and managing views in PostgreSQL. We will introduce you to the concept of views, and show how views are created, modified, and removed. And most importantly we will also see how views can be implemented in DBMS.
A view in PostgreSQL can be defined as a virtual table that is derived from underlying base tables or previously defined views.
It is important to note the following points:
- Virtual Table: A view does not necessarily exist in its physical form i.e. its tuples are not always physically stored in the database.
- No Data Storage: Views do not store data themselves, which can limit certain update operations.
- Unlimited Querying: There are absolutely no limitations on querying a view.
Creating PostgreSQL Views
To specify a view, we use a create view statement.
Syntax:
CREATE [TEMP |TEMPORARY] VIEW view_name AS
SELECT column1, column2....
FROM table_name
WHERE [condition];
- The view is assigned a virtual table name, a list of attributes, and a query that defines its content.
- The ‘TEMP’ or ‘TEMPORARY’ option allows you to create a temporary view that exists only for the duration of the session.
Example: Consider the following tables:

BOOK TABLE

BOOK COPIES

Book_Authors
Now, from the following tables, suppose we wish to create a view such that we can calculate the number of copies for each book given in the book table.
This can be done as follows:
CREATE VIEW BOOKCOUNT AS
SELECT Book_id,Title, Sum(No_of_copies)
FROM BOOK A, BOOK_COPIES B
WHERE A.Book_id = B.Book_ID
GROUP BY A.BOOK_ID;
SELECT * FROM BOOKCOUNT;
The output of the following code will be:

BOOKCOUNT
Updating PostgreSQL Views
We can change the defining query of a view, we use the CREATE VIEW statement with OR REPLACE addition as follows:
CREATE OR REPLACE VIEW VIEW_NAME AS
SELECT column1, column2....
FROM table_name
WHERE [condition];
An update on a view is generally not feasible because it has some side effects on the underlying base tables. For example,if we update the sum of copies of a particular book we will have to update the corresponding number of book count in the bookcount table.
Therefore, we can say that:
- A view with a single defining table is updatable if the view attributes contain the primary key of the base relation as well as other attributes that do not have default values specified.
- Views that are defined by the natural join of multiple tables are not updatable.
- Views defined using grouping and aggregate functions are not updatable.
WITH CHECK OPTION
This clause can basically be added at the end of the view definition if the view has to be updated by the INSERT, UPDATE,DELETE statements.It will allow the system to reject operations that violate the SQL rules for view updates.
In the given view BOOKCOUNT that we have created ,it is not updatable.
If we try to update BOOKCOUNT view we will get the following error:

Update BOOKCOUNT set title ='ADP' where Book_ID =1;
ERROR:The target table BOOKCOUNT is not updatable.
In fact any of the views that contain the following elements are not updatable:
- Aggregate functions such as MIN, MAX, SUM, AVG, and COUNT.
- DISTINCT
- GROUP BY clause.
- HAVING clause.
- UNION or UNION ALL clause.
- Left join or outer join.
- Sub query in the SELECT clause or in the WHERE clause that refers to the table appeared in the FROM clause.
- Reference to non-updatable view in the FROM clause.
- Reference only to literal values.
- Multiple references to any column of the base table
So we will create another view
CREATE VIEW Example as
SELECT title, Author_Name,
FROM BOOK, BOOK_AUTHORS
WHERE BOOK.Book_ID=BOOK_AUTHORS.Book_ID;
The created view will be:

Example
Now let’s update this view that we have created.
Updating PostgreSQL Views
UPDATE Example
SET Author_Name='Rahul'
WHERE Author_Name='Abhishek';
The updated view will look like this:

Example
Deleting PostgreSQL Views
To remove an existing view in PostgreSQL, we can use DROP VIEW statement as follows:
DROP VIEW [ IF EXISTS ] view_name;
If a view name specified does not exist then obviously it would result in an error.So, in order to avoid facing errors we use the IF EXISTS option .
Example: To remove the view that we have created we will use the following command
DROP VIEW IF EXISTS BOOKCOUNT;
Let’s check the output for the same

After deleting the view ,when we try to retrieve the columns from the view , it gives an error saying that the referenced view doesn’t exist.
Similar Reads
PostgreSQL - Materialized Views
Materialized Views in PostgreSQL are a powerful tool that can significantly enhance query performance by physically storing the result of a complex query. Unlike standard views, which compute their result set every time they are queried, materialized views store the query result and can be refreshed
3 min read
PostgreSQL - GROUPING SETS
In PostgreSQL, the GROUPING SETS feature allows users to generate result sets that are equivalent to those produced by the UNION ALL of multiple GROUP BY clauses. This feature is highly useful for creating complex reports with multiple levels of aggregation in a single query. A grouping set is essen
3 min read
PostgreSQL - SELF JOIN
In PostgreSQL, a SELF JOIN is a powerful technique that allows us to join a table with itself. This type of join is particularly useful for comparing rows within the same table, such as establishing hierarchical relationships or identifying duplicate records. Unlike other joins, there is no specific
4 min read
PostgreSQL - SQL Optimization
PostgreSQL is the most advanced general-purpose open source database in the world. pgAdmin is the most popular management tool or development platform for PostgreSQL. It is also an open source development platform. It can be used in any Operating Systems and can be run either as a desktop applicatio
5 min read
PostgreSQL - Psql commands
PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively. Here, we highlight some of t
2 min read
PostgreSQL - SELECT INTO
The PostgreSQL SELECT INTO statement allows users to create a new table directly from the result set of a query. This command is ideal for duplicating or organizing data from an existing table into a new one for further analysis. SELECT INTO does not return data to the client but saves it in a new t
4 min read
PostgreSQL - Select Into
In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword. The select into statement will assign the data returned by th
2 min read
PostgreSQL - Alias
PostgreSQL aliases are powerful tools that allow you to assign temporary names to tables or columns within your queries. These aliases only exist during the execution of the query, making your SQL code more readable and efficient. What is a PostgreSQL Alias?An alias in PostgreSQL is a temporary name
2 min read
PostgreSQL - Joins
The PostgreSQL JOIN statement is a powerful tool for combining data or rows from one or more tables based on a common field between them. These common fields are typically the primary key in the first table and the foreign key in the other table(s). By using different types of JOINs, we can perform
5 min read
PostgreSQL - Create updatable Views
Views in PostgreSQL provide a way to represent a subset of a real table, selecting certain columns or rows from an ordinary table. They are particularly useful for restricting access to the original table, allowing users to see only a specific portion of it. The table from which a view is created is
3 min read