In today’s article, What is a PostgreSQL Temporary Table? and How to Create? We will inform you about the subject.
Most of the things I explained about the table are also valid here, but the Temp(temporary) table is session-based, so you opened the query window to write a query and created the table there, but if you need this temp table again in a different session, you cannot access it and your temp table will be deleted when you close the session.
Temp tables are written to the default tablespace of the created database.
Let’s create a temporary table and perform operations on it.
1 | CREATE TEMPORARY TABLE customers( id int ,m_no int ,name text,surname text); |
We created the Temp Table and let’s add data to it
1 | insert into customers(id,m_no,name,surname) values(1,1,'Faruk','ERDEM'); |
Let’s check if this is the data we added.
As we can see, the data has been added to our temp table.
Let’s query using the \dt command that PostgreSQL created for convenience.
We see above that it is written as pg_temp_3 under the schema.
When we want to delete the table, we can end the session or delete the table with the help of the following command.
1 | DROP TABLE customers; |