PostgreSQL – When Temporary Tables Are Deleted

postgresqltemporary-tables

I am inserting data into temp table and its working fine. below is the psedo sql code for the same

with cte as(

)
select * 
into temp_table
from cte

with this approach data is inserted very fast into temp table. As per my knowleged temp table is deleted once the session is closed. but my temp table is not deleted even if closed my pgadmin connection.

My question is does temp table in postgresql are deleted automatically or they remains on disk space until we delete them.

Regards,

Sanjay Salunkhe

Best Answer

In fact you created a regular table. You have to specify that this is a temporary table:

with cte as(
-- <a_query>
)
select * 
into temporary temp_table
from cte;

or (the recommended syntax):

create temporary table temp_table as
-- <a_query>

See SELECT INTO and CREATE TABLE AS.