In PostgreSQL, A temporary table can have the same name as of a permanent table, even though it is not recommended. When the user creates a temporary table with the same name as a permanent table, it cannot access the permanent table until the temporary table is removed.
Example:
First, create a table named customers
:
CREATE TABLE customers(id SERIAL PRIMARY KEY, name VARCHAR NOT NULL);
Second, create a temporary table with the same name: customers
:
CREATE TEMP TABLE customers(customer_id INT);
Now query the data from the customers table as below:
SELECT * FROM customers;
Output:
At this stage, PostgreSQL accessed the temporary table customers instead of the permanent one. From now on, you can only access the permanent customers
table in the current session when the temporary table customers
is removed specifically.If you list the tables in the test
database, you will only see the temporary table customers, not the permanent ones below:

Note: PostgreSQL creates temporary tables in a special schema, therefore, you must not specify the schema in the CREATE TEMP TABLE statement.
Now if you list the tables using the below command:
\dt
The result is as shown below:

The output shows that the schema of the customers
temporary table is pg_temp_3
.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Aug, 2020
Like Article
Save Article