Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Create a View in PostgreSQL
In this tutorial, you’ll learn how to create, use, and manage views in PostgreSQL to simplify your queries and improve database organization.
What is a View?
A view in PostgreSQL is a virtual table that is based on the result of a SQL query. It acts like a table but does not store data physically. Instead, it dynamically presents data when queried, making it useful for simplifying complex SQL and enhancing security.
Benefits of Using Views
- Simplifies complex SQL queries
- Improves query readability
- Enhances data security by exposing only specific columns
- Provides a layer of abstraction over base tables
How to Create a View
Here’s the basic syntax:
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;Example
Let’s create a view named high_salary_employees that shows employees earning more than $70,000:
CREATE VIEW high_salary_employees AS
SELECT id, name, salary
FROM employees
WHERE salary > 70000;Querying a View
You can query a view just like a table:
SELECT * FROM high_salary_employees;Updating a View
If you need to change the definition of a view, use CREATE OR REPLACE VIEW:
CREATE OR REPLACE VIEW high_salary_employees AS
SELECT id, name, salary, department
FROM employees
WHERE salary > 70000;Dropping a View
To remove a view from the database:
DROP VIEW high_salary_employees;Tips
- Avoid using
SELECT *in views to improve performance and stability. - Name views meaningfully to reflect their purpose.
- Use views to enforce access control by limiting sensitive columns.
Turn this PostgreSQL query into a scheduled report
DataReportive runs your SQL queries on a schedule and delivers the results via email, PDF, Excel, or HTML. Set up your reports once and get fresh data delivered automatically.
Automate PostgreSQL reporting with DataReportive
Turn your PostgreSQL queries into scheduled reports delivered to your team or customers.