Open In App

PostgreSQL – Temporary table name

Improve
Improve
Like Article
Like
Save
Share
Report

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.


Last Updated : 28 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads