PostgreSQL – Temporary Table
Last Updated :
04 Nov, 2024
A PostgreSQL temporary table is a powerful tool for managing session-specific data that only needs to exist for a short duration. Temporary tables are created within a database session or transaction and are automatically dropped when the session ends, making them ideal for intermediate data storage.
In this PostgreSQL temporary table tutorial, we’ll explore how to create and manage temporary tables effectively. This guide includes examples to help us understand how to create a temporary table in PostgreSQL and use it efficiently.
PostgreSQL Temporary Table
Temporary tables can be utilized in various scenarios, such as when executing complex queries that require multiple steps or when isolating data for specific transactions. They ensure that the data remains private to the session that created it, preventing any conflicts or visibility issues with other users or sessions.
Syntax
CREATE TEMPORARY TABLE temp_table(
...
);
or,
CREATE TEMP TABLE temp_table(
...
);
Both TEMPORARY
and TEMP
keywords serve the same purpose, allowing us to define a table that will exist only within the session. A temporary table is visible only to the session that creates it, remaining invisible to other sessions.
PostgreSQL Temporary Table Example
In this section, we will provide a practical example of how to create and use a temporary table in PostgreSQL. This example will demonstrate the steps involved in creating a temporary table, inserting data, and observing its session-specific behavior.
Step 1: Create a New Database
To get started, we need a dedicated environment for our operations. First, create a new database named test
. This is done using the following command:
Query:
CREATE DATABASE test;
Explanation:
This command initializes a new database where we can perform our operations. It’s essential to have a separate database for testing and experimentation, ensuring that we don’t interfere with existing data.
Step 2: Create a Temporary Table
Once the database is set up, connect to it and create a temporary table named mytemp
. The command to create the temporary table is:
Query:
CREATE TEMP TABLE mytemp(c INT);

Explanation:
In this step, we define a temporary table with a single column c
of type INT
. This table will exist only for the duration of the current session, allowing us to store integer values temporarily.
Step 3: Query the Temporary Table from Another Session
Next, let’s see the session-specific nature of temporary tables. Then, launch another session that connects to the ‘test’ database and query data from the ‘mytemp’ table:
Query:
SELECT * FROM mytemp;
Explanation:
When we run this command in a new session, PostgreSQL will return an error indicating that the mytemp
table does not exist. This behavior demonstrates the isolation of temporary tables; they are not accessible from sessions that did not create them.
Step 4: Ending the Session
To illustrate the behavior of temporary tables further, we should now quit all sessions. We can exit from the PostgreSQL prompt using the following command:
Query:
\q
Explanation:
When we end the session that created the mytemp
table, PostgreSQL automatically drops the table. Therefore, it will no longer be available for querying in any future sessions.
Step 5: Reconnect and Query the Temporary Table
Finally, login to the database server again and query data from the ‘mytemp’ table:
SELECT * FROM mytemp;
Output

Explanation:
The ‘mytemp'
table does not exist because it has been dropped automatically when the session ended, therefore, PostgreSQL issued an error.
Important Points About PostgreSQL Temporary Table
- A temporary table is only visible to the session that creates it. Other sessions cannot access or see the temporary table.
- PostgreSQL drops temporary tables automatically at the end of the session or transaction, ensuring no leftover temporary data occupies space in the database.
- Each session’s temporary tables are isolated, preventing conflicts and ensuring data security.
- Be mindful of the session scope when creating temporary tables to avoid errors and ensure proper usage.
Conclusion
In conclusion, temporary tables in PostgreSQL offer a unique advantage by existing only during the duration of a session, providing a convenient way to handle session-specific data without affecting other users. Unlike permanent tables, which persist until explicitly dropped, temporary tables are automatically removed at the end of the session, ensuring a clean slate for each database interaction.
Similar Reads
PostgreSQL - Temporary table name
In PostgreSQL, it is possible, though not recommended, to create a temporary table with the same name as an existing permanent table. When a temporary table shares its name with a permanent table, the temporary table will take precedence, preventing access to the permanent table until the temporary
2 min read
PostgreSQL - Removing Temporary Table
In PostgreSQL, one can drop a temporary table by the use of the DROP TABLE statement. Syntax: DROP TABLE temp_table_name; Unlike the CREATE TABLE statement, the DROP TABLE statement does not have the TEMP or TEMPORARY keyword created specifically for temporary tables. To demonstrate the process of d
2 min read
SQL Server TEMPORARY TABLE
A temporary SQL table also known as a temp table is created in the database only for a specific session to store the data temporarily which is needed for a short duration of time. In this article, we are going to learn about SQL Server temporary tables, their types, how to use them, and why they are
5 min read
MySQL Temporary Table
Do you want to store the intermediate results of your query in some table but only for a given DB session and not persist for the lifetime? Is there some way to hold temporary data for a short time without making it permanent on the database forever? If you have ever thought about this and wondered
5 min read
PostgreSQL - TRUNCATE TABLE
When working with large datasets in PostgreSQL, you might find the need to clear all data from a table quickly. While the DELETE statement is a common option, it can be slow when dealing with a table containing large amounts of data. Instead, PostgreSQL provides the TRUNCATE TABLE statement, which i
2 min read
PostgreSQL - TRUNCATE TABLE
In PostgreSQL, the TRUNCATE TABLE statement provides a quick and efficient way to remove all data from large tables, freeing up storage space instantly. Unlike the DELETE statement, which removes rows one by one and logs each deletion, TRUNCATE TABLE is optimized for performance, especially with lar
4 min read
What is Temporary Table in SQL?
A temporary table in SQL is an important tool for maintaining intermediate results during query execution. They help store temporary data without affecting the underlying permanent tables. In this article, weâll explore temporary tables in SQL, their types (local vs. global), and how to use them eff
3 min read
SQL | Declare Local Temporary Table
Local temporary tables in SQL are powerful for managing intermediate data during a session or procedure. These tables enable developers to temporarily store results, perform computations, and simplify complex queries. Understanding how to declare, use, and manage local temporary tables effectively c
5 min read
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 - Copy Table
In PostgreSQL, the copy table functionality is a powerful feature that allows us to efficiently duplicate existing tables, including their structure and data. This capability is essential for various database management tasks such as backing up data, migrating tables, or testing modifications in a s
4 min read