The PostgreSQL Truncate Table command is used to remove all rows from the existing table. It works in the same way as unqualified DELETE but Truncate does not scan each table that is why it is faster. Once the Truncate statement is complete, the space that the table has occupied is immediately available. The Truncate helps on the large tables. To perform the PostgreSQL Truncate operation, we should have the Truncate permission granted on the table.

In the following section, we will use the TRUNCATE TABLE command to delete all rows from the table PRODUCTS_DETAIL. This table is created in the create and drop table section of the tutorial.

The following is the syntax of PostgreSQL Truncate Table command.


Syntax:

TRUNCATE TABLE table_name;

Now let's use the following Truncate Table command to delete records from the table PRODUCTS_DETAIL. First, we will use the SELECT * from PRODUCTS_DETAIL; SQL to verify the records of this table and then we will use the Truncate Table command to delete the rows.


Command:

postgres=# SELECT * FROM PRODUCTS_DETAIL;
postgres=# TRUNCATE TABLE PRODUCTS_DETAIL;


Output:

postgresql truncate command

We can run the SELECT * FROM PRODUCTS_DETAIL to verify the operation and we see that all rows are deleted and only the table structure is left.

postgres=# SELECT * FROM PRODUCTS_DETAIL;


Output:

postgresql truncate command verification