Easy Tutorial
❮ Postgresql Functions Postgresql Group By ❯

PostgreSQL TRUNCATE TABLE

In PostgreSQL, TRUNCATE TABLE is used to delete the data from a table but retains the table structure.

Alternatively, you can use DROP TABLE to delete a table, but this command will also remove the table structure. If you want to insert data again, you would need to recreate the table.

PostgreSQL VACUUM operation is used to reclaim and reuse the disk space occupied by updated/deleted rows.

Syntax

The basic syntax for TRUNCATE TABLE is as follows:

TRUNCATE TABLE table_name;

Example

Create a COMPANY table (download COMPANY SQL file), with the following data content:

tutorialprodb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

The following example uses TRUNCATE TABLE to clear the COMPANY table:

tutorialprodb=# TRUNCATE TABLE COMPANY;

The result is as follows:

tutorialprodb=# SELECT * FROM CUSTOMERS;
 id | name | age | address | salary
----+------+-----+---------+--------
(0 rows)
❮ Postgresql Functions Postgresql Group By ❯