PostgreSQL - CREATE TABLE AS
Last Updated :
05 Aug, 2024
The CREATE TABLE AS statement in PostgreSQL is a powerful tool used to create a new table and populate it with data returned by a query. This functionality allows you to generate tables on the fly based on query results, which can be very useful for reporting, analysis, and other tasks.
Let us better understand the CREATE TABLE AS statement in PostgreSQL from this article.
Syntax
CREATE TABLE new_table_name AS query;
Parameters
Let's analyze the above syntax:
- CREATE TABLE new_table_name: First, specify the new table name after the CREATE TABLE clause.
- AS query: Finally, provide a query whose result set is added to the new table after the AS keyword.
Temporary Tables
The 'TEMPORARY' or 'TEMP' keyword allows you to create a temporary table. Temporary tables are automatically dropped at the end of a session or a transaction, making them ideal for transient data that does not need to persist.
Syntax:
CREATE TEMP TABLE new_table_name AS query;
Unlogged Tables
The 'UNLOGGED' keyword creates an unlogged table. Unlogged tables are faster because they do not write data to the write-ahead log, but they are not crash-safe and their contents are lost in the event of a crash.
Syntax:
CREATE UNLOGGED TABLE new_table_name AS query;
Specifying Column Names
The columns of the new table will have the names and associated with the output columns of the clause.If you want the table columns to have different names, you can specify the new table columns after the new table name as below.
Syntax:
CREATE TABLE new_table_name ( column_name_list) AS query;
Using IF NOT EXISTS
In case one wants to avoid an error by creating a new table that already exists, you can use the 'IF NOT EXISTS' option as follows.
Syntax:
CREATE TABLE IF NOT EXISTS new_table_name AS query;
PostgreSQL CREATE TABLE AS Statement Examples
Let us take a look at some of the examples of CREATE TABLE AS Statement in PostgreSQL to better understand the concept. For examples we will be using the sample database (ie, dvdrental).
Example 1: Creating a Table of Action Films
In this example, we will use the 'film' and 'film_category' tables to create a table of action films that belong to category 1.
CREATE TABLE action_film AS
SELECT
film_id,
title,
release_year,
length,
rating
FROM
film
INNER JOIN film_category USING (film_id)
WHERE
category_id = 1 ;
Now we can verify the new table using the below statement:
SELECT * FROM action_film ORDER BY title;
Output:

Example 2: Creating a Table with Summary Data
In this example, we will create a new table 'film_rating' and populate it with summary data from the 'film' table.
CREATE TABLE IF NOT EXISTS film_rating (rating, film_count) AS
SELECT
rating,
COUNT (film_id)
FROM
film
GROUP BY
rating;
Now to verify the new table, use the below statement:
SELECT * FROM film_rating;
Output:

Important Points About PostgreSQL CREATE TABLE AS Statement
- Use the TEMPORARY or TEMP keyword to create a table that exists only for the duration of the session.
- Use the UNLOGGED keyword to create a table that is not logged in the write-ahead log, offering better performance at the cost of crash-safety.
- Use the IF NOT EXISTS clause to avoid an error if the table already exists.
- The new table does not inherit indexes or constraints from the source table. These need to be added manually if required.
Similar Reads
PostgreSQL Tutorial
In this PostgreSQL tutorial youâll learn the basic data types(Boolean, char, text, time, int etc.), Querying and Filtering techniques like select, where, in, order by, etc. managing and modifying the tables in PostgreSQL. Weâll cover all the basic to advance concepts of PostgreSQL in this tutorial.
8 min read
PostgreSQL DATEDIFF Function
PostgreSQL doesnât have a DATEDIFF function like some other databases, but you can still calculate the difference between dates using simple subtraction. This approach allows you to find out how many days, months, or years separate two dates. In this article, we'll explore how to compute date differ
6 min read
PostgreSQL - Data Types
PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w
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 th
2 min read
Top 50 PostgreSQL Interview Questions and Answers
Are you preparing for a PostgreSQL interview? PostgreSQL is a powerful open-source relational database management system (RDBMS) that is well-known for its reliability, scalability, and rich set of features. Itâs a favorite among developers and businesses alike, making it essential to master if we w
15+ min read
How to Dump and Restore PostgreSQL Database?
PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw
6 min read
PostgreSQL - Create Database
Creating a database in PostgreSQL is an important task for developers and database administrators to manage data effectively. PostgreSQL provides multiple ways to create a database, catering to different user preferences, whether through the command-line interface or using a graphical interface like
5 min read
PostgreSQL - SERIAL
When working with PostgreSQL, we need to create tables with unique primary keys. PostgreSQL offers a powerful feature known as the SERIAL pseudo-type which simplifies generating auto-incrementing sequences for columns. In this article, weâll learn about the PostgreSQL SERIAL pseudo-type by explain h
5 min read
PostgreSQL Connection String
A connection string is an essential component that enables applications to communicate with databases or other data sources by providing the necessary configuration details. It consolidates critical information such as the server address, database name, user credentials, and additional parameters li
4 min read
PostgreSQL - IF Statement
PostgreSQL IF statement is an essential tool for implementing conditional logic within SQL queries and stored procedures. It allows developers to execute different actions based on specific conditions and enhances the flexibility of database operations. In this article, we will explore various Postg
5 min read