PostgreSQL - TRUNCATE TABLE
Last Updated :
20 Aug, 2024
When working with large datasets in PostgreSQL, you might find the need to clear all data from a table quickly. While the DELETE statement is a common option, it can be slow when dealing with a table containing large amounts of data. Instead, PostgreSQL provides the TRUNCATE TABLE statement, which is significantly faster and more efficient.
Let us get a better understanding of the TRUNCATE TABLE Statement in PostgreSQL from this article.
What is the TRUNCATE TABLE Statement?
The TRUNCATE TABLE statement is used to instantly remove all rows from one or more tables. Unlike the DELETE statement, which removes rows one at a time and logs individual row deletions, TRUNCATE TABLE operates by deallocating the table's storage space directly, making it a much faster operation. It’s an ideal choice when you want to reset a table to its empty state without worrying about the overhead of row-by-row deletion.
Syntax
TRUNCATE TABLE table_name;
The TRUNCATE TABLE statement does not care about the table while deleting the same, simultaneously clearing the space for use by the user.
PostgreSQL TRUNCATE TABLE Example
Let us look at an example of the TRUNCATE TABLE in PostgreSQL to better understand the concept.
First list all tables in the database using the below command:
\dt
This will list all tables as depicted below:

The following example uses the TRUNCATE TABLE statement to delete all data from the 'foo' table:
TRUNCATE TABLE foo;
Now if checked for the table again it will show that all data in the table is deleted as shown below:
SELECT * FROM foo;
Output:

Advantages of Using TRUNCATE TABLE
- TRUNCATE TABLE is much faster than DELETE, making it the preferred choice for large tables.
- The command frees up the table’s disk space immediately, ensuring optimal space usage.
- TRUNCATE TABLE is minimally logged as compared to DELETE, reducing the time spent writing to the transaction log.
Points to Remember
- Since TRUNCATE doesn’t operate on individual rows, row-level triggers won’t fire.
- If your table has foreign key dependencies, you must handle them with CASCADE, or manually remove dependent rows before truncating.
Similar Reads
PostgreSQL - Temporary Table A PostgreSQL temporary table is a powerful tool for managing session-specific data that only needs to exist for a short duration. Temporary tables are created within a database session or transaction and are automatically dropped when the session ends, making them ideal for intermediate data storage
4 min read
PostgreSQL - Rename Table Renaming a table in PostgreSQL is a common task that can be quickly done using the RENAME clause in combination with the ALTER TABLE statement. This article will walk you through the process of renaming an existing table in PostgreSQL, explaining the syntax, and providing a detailed example.SyntaxAL
2 min read
PostgreSQL - DROP TABLE In PostgreSQL, the DROP TABLE statement is a powerful and permanent command used to delete one or more tables from a database. Since this operation cannot be undone, it is essential to understand how to use it safely and to be aware of its options to prevent accidental data loss. In this article, we
5 min read
PostgreSQL - ALTER TABLE In PostgreSQL, the ALTER TABLE statement is a powerful and essential tool that allows us to modify the structure of an existing table to meet evolving database needs. With PostgreSQL ALTER TABLE, we can perform various modifications on the table without disrupting the ongoing operations of our datab
6 min read
PostgreSQL - Removing Temporary Table In PostgreSQL, one can drop a temporary table by the use of the DROP TABLE statement. Syntax: DROP TABLE temp_table_name; Unlike the CREATE TABLE statement, the DROP TABLE statement does not have the TEMP or TEMPORARY keyword created specifically for temporary tables. To demonstrate the process of d
2 min read