How to CREATE VIEW in PostgreSQL

Galaxy Glossary

How do I create a view in PostgreSQL?

CREATE VIEW saves a SELECT statement as a virtual table, letting you query complex joins through a simple object.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

What does CREATE VIEW do in PostgreSQL?

CREATE VIEW stores a SELECT statement under a name. The view behaves like a read-only virtual table, recalculating on each access. It reduces duplicate logic, shortens queries, and masks sensitive data.

How do I write the basic CREATE VIEW syntax?

Use CREATE [ OR REPLACE ] [ TEMP ] VIEW view_name [ (col1, …) ] AS select_query [ WITH [ CASCADED | LOCAL ] CHECK OPTION ];. Add TEMP for session-only views, supply column aliases when the SELECT uses expressions, and append WITH CHECK OPTION to block rows that violate the view filter on INSERT/UPDATE.

When should I use a view in an ecommerce schema?

Create views for repetitive reports—customer lifetime value, daily revenue, or low-stock products. Stakeholders query one object instead of rewriting joins between Customers, Orders, OrderItems, and Products.

Can I secure data with views?

Yes. Grant SELECT on the view to analysts while withholding table permissions. The view surfaces only approved columns, perfect for exposing order summaries without customer emails.

How do I modify or drop a view safely?

Run CREATE OR REPLACE VIEW to update the definition without breaking dependent queries. Remove it with DROP VIEW IF EXISTS view_name; to avoid errors if the view is already gone.

What best practices should I follow?

1) Name views with a noun phrase like daily_revenue.
2) Avoid SELECT *; list columns to prevent accidental schema leaks.
3) Add comments using COMMENT ON VIEW for discoverability.

What common mistakes occur with CREATE VIEW?

Ambiguous column names break consumers—always alias expressions. Forgetting OR REPLACE blocks deployments; include it when views may already exist.

Why How to CREATE VIEW in PostgreSQL is important

How to CREATE VIEW in PostgreSQL Example Usage


-- Show customers who spent over $1,000 in total
a SELECT FROM THE VIEW
SELECT *
FROM   customer_order_totals
WHERE  lifetime_value > 1000
ORDER  BY lifetime_value DESC;

How to CREATE VIEW in PostgreSQL Syntax


CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW view_name
    [ (column_name [, ...]) ]
AS
    select_statement
[ WITH [ CASCADED | LOCAL ] CHECK OPTION ];

-- Parameters
OR REPLACE          Redefines an existing view.
TEMP | TEMPORARY     Drops the view at session end.
column_name list     Overrides derived column names.
CHECK OPTION         Prevents writes that violate the view filter.

-- Ecommerce example
CREATE VIEW customer_order_totals AS
SELECT c.id   AS customer_id,
       c.name AS customer_name,
       SUM(o.total_amount) AS lifetime_value
FROM   Customers c
JOIN   Orders o ON o.customer_id = c.id
GROUP  BY c.id;

Common Mistakes

Frequently Asked Questions (FAQs)

Does a view store data?

No. A standard view recalculates each time you query it. Use MATERIALIZED VIEW if you need persisted data.

Can I update rows through a view?

You can if the SELECT targets a single base table and omits aggregates. Add WITH CHECK OPTION to enforce the view filter on writes.

How do I give analysts access only to the view?

GRANT SELECT ON view_name TO analyst_role; REVOKE ALL ON underlying tables FROM analyst_role; The role now sees only the view.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.