EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Temporary Table

By Sohel SayyadSohel Sayyad

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Temporary Table

PostgreSQL Temporary Table

Introduction to PostgreSQL Temporary Table

PostgreSQL has a very useful database feature that has the ability to create temporary tables for a current transaction or for the database session. It is helpful in managing the unprocessed data. As the name specifies, the temporary table has a short life span, and it is not getting stored permanently; every time, we have to create a temporary table for each transaction or database session. The temporary table exists in the database for a particular database session duration or in the current transaction. The temporary tables are automatic gets dropped at the end of the current transaction or at the end of the database session. The temporary tables are invisible to other transactions and database sessions.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

In order to create a temporary table, we have to use the CREATE TEMPORARY TABLE statement as follows:

CREATE TEMPORARY TABLE temp_table_name(  … );

Also, we can use the TEMP keyword instead of the TEMPORARY keyword in the CREATE statement defined above and can rewrite it as follows:

CREATE TEMP TABLE temp_table_name( … );

How Temporary Table works in PostgreSQL?

  • The temporary tables are invisible to other transactions and database sessions.
  • The temporary tables are visible to the current transaction or database session in which we create the table.
  • We can create a temporary table with the same name as a permanent table in the database, which is actually not recommended. The temporary table hides the permanent table is defined with the same name until it is not dropped for the respective database session or transaction.
  • We can not access the permanent table if we are having a temporary table with the same name as a permanent table.

How to Create a PostgreSQL Temporary Table?

We can use either the TEMP or TEMPORARY keyword with CREATE table statement to create a temporary table.

Consider the following example, which creates two tables, ‘student’ and ‘teacher’, with the help of TEMP and TEMPORARY keyword with CREATE TABLE statements, respectively.

1. With TABLE Keyword

CREATE TEMP TABLE student(stud_id serial NOT NULL PRIMARY KEY, stud_name VARCHAR(80));

Illustrate the created table using the above statement with the help of the following statement and syntax:

select * from student;

2. With TEMPORARY Keyword

CREATE TEMPORARY TABLE teacher(teacher_id serial NOT NULL PRIMARY KEY, teacher_name VARCHAR(80));

Illustrate the created table using the above statement with the help of the following statement and syntax:

Select * from teacher;

Now, consider that we have one table named ‘users’ that already exists in the database, which permanent table.

We will create the permanent ‘users’ table by using the following CREATE table statement.

CREATE TABLE users(id int, name VARCHAR(80));

Now, we will add some data into the permanent ‘users’ table. The following INSERT INTO statement will add some records in the permanent ‘users’ table.

INSERT INTO users(id, name) VALUES (1,'Oliver'), (2,'Jake'),  (3,'Harry');

Illustrate the result of the above statement by using the following snapshot and SQL statement.

Select * from users;

Output:

PostgreSQL Temporary Table 1

We will create a temporary table with the same name ‘users’ as the permanent table we have created.

We will create the temporary ‘users’ table by using the following CREATE table statement.

CREATE TEMPORARY TABLE users(id int, name VARCHAR(80));

Now, we will add some data into the temporary ‘users’ table. The following INSERT INTO statement will add some records in the temporary ‘users’ table.

INSERT INTO users(id, name) VALUES (4, 'Callum');

Now we have a users table which is permanent as well as temporary, but in PostgreSQL, the temporary table hides the definition of the permanent table within the transaction or database session.

Here, we will try to get all records from the user’s table using the following statement.

select * from users;

Output:

Illustrate the result of the above statement by using the following snapshot.

PostgreSQL Temporary Table 2

In the above snapshot, we can see the result set returned is from the temporary user’s table, not from the permanent user’s table.

How to Insert Data in PostgreSQL Temporary Table?

Now, we will add some data into a temporary table created in the CREATE TABLE section.

The following INSERT INTO statement will add some records in both ‘student’ and ‘teacher’ tables.

INSERT INTO student(stud_name) VALUES ('Oliver'), ('Jake'), ('Harry'), ('Callum');

Illustrate the result of the above statement by using the following snapshot and SQL statement.

select * from student;

Output:

insert data

INSERT INTO teacher(teacher_name) VALUES ('Jack'), ('Connor'), ('Jacob'), ('John');

Illustrate the result of the above statement by using the following snapshot and SQL statement.

select * from teacher;

Output:

teacher_id

How to Delete PostgreSQL Temporary Table?

In order to drop a temporary table, we use the DROP TABLE statement as follows.

DROP TABLE temp_table_name;

Consider the following example, which will delete both the ‘student’ and ‘teacher’ tables created in the CREATE table section above:

The following statement will delete the student table.

DROP TABLE student;

The following statement will delete the teacher table.

DROP TABLE teacher;

It is not necessary to mention the TEMP or TEMPORARY keyword for dropping a table like defining these keywords for the CREATE TABLE statement.

Conclusion

Here we have seen how to use the PostgreSQL Temporary tables and how the PostgreSQL Temporary tables work to store the data. Also, we have added some examples of PostgreSQL Temporary tables.

Recommended Articles

This is a guide to PostgreSQL Temporary Table. Here we discuss how temporary table works, create, insert and delete in PostgreSQL with examples. You may also have a look at the following articles to learn more –

  1. PostgreSQL UNIQUE Constraint
  2. PostgreSQL NATURAL JOIN
  3. PostgreSQL Full Join
  4. PostgreSQL REGEXP_MATCHES()
  5. PostgreSQL Primary Key
  6. PostgreSQL Tablespaces?

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More