9951 explained code solutions for 126 technologies


postgresqlHow do I create a materialized view in PostgreSQL?


Creating a materialized view in PostgreSQL is fairly straightforward. The basic syntax for creating a materialized view is as follows:

CREATE MATERIALIZED VIEW view_name AS
SELECT columns
FROM tables
WHERE conditions;

The above code will create a materialized view with the name view_name that contains the columns specified in the SELECT statement. The FROM clause specifies the tables to query and the WHERE clause specifies the conditions to filter the data.

For example, the following code creates a materialized view called sales_summary that contains a summary of sales data:

CREATE MATERIALIZED VIEW sales_summary AS
SELECT product_id, SUM(quantity) AS total_sales
FROM sales
WHERE date > '2020-01-01'
GROUP BY product_id;

The code creates the materialized view sales_summary that contains the product_id and the total sales for each product since the beginning of 2020.

The following parts are used in the code:

  • CREATE MATERIALIZED VIEW: This is used to create a materialized view.
  • view_name: This is the name of the materialized view.
  • SELECT columns: This is used to specify the columns to include in the materialized view.
  • FROM tables: This is used to specify the tables to query.
  • WHERE conditions: This is used to specify the conditions to filter the data.
  • GROUP BY: This is used to group the data.

Helpful links

Edit this code on GitHub