How to Truncate Tables in PostgreSQL

Avatar

By squashlabs, Last Updated: October 30, 2023

How to Truncate Tables in PostgreSQL

When working with PostgreSQL, there may be scenarios where you want to remove all data from multiple tables at once. This can be useful when setting up a fresh database or when you need to clear out data for testing purposes. One efficient way to achieve this is by using the TRUNCATE function in PostgreSQL.

The TRUNCATE function is a fast and efficient way to remove all data from a table. It differs from the DELETE statement in that it does not generate any undo logs, making it much faster for large tables. The TRUNCATE function also resets any auto-incrementing sequences associated with the table.

The Truncate Function

The TRUNCATE function in PostgreSQL is used to quickly remove all rows from a table. It is a DDL (Data Definition Language) statement, meaning it changes the structure of the table rather than manipulating the data itself. Here is the syntax for the TRUNCATE function:

TRUNCATE TABLE table_name;

To truncate all tables in a PostgreSQL database, you can use the following query:

DO $$ DECLARE
    table_name text;
BEGIN
    FOR table_name IN (SELECT tablename FROM pg_tables WHERE schemaname='public') LOOP
        EXECUTE 'TRUNCATE TABLE ' || table_name || ' CASCADE;';
    END LOOP;
END $$;

This query uses a cursor to iterate over all tables in the public schema and executes the TRUNCATE statement for each table. The CASCADE option is used to automatically truncate any dependent tables as well.

Related Article: Executing Queries to Remove Duplicate Rows in PostgreSQL

Comparing Truncate and Delete in PostgreSQL

While both the TRUNCATE and DELETE statements can be used to remove data from tables in PostgreSQL, there are some key differences between them.

The TRUNCATE statement is faster and more efficient than the DELETE statement when removing all data from a table. This is because TRUNCATE does not generate any undo logs, making it a non-logged operation. On the other hand, the DELETE statement generates undo logs and can be slower for large tables.

Another difference is that the TRUNCATE statement resets any auto-incrementing sequences associated with the table, while the DELETE statement does not. This can be useful if you want to start fresh with new data.

Here is an example that demonstrates the difference between TRUNCATE and DELETE:

-- Truncate the table
TRUNCATE TABLE employees;

-- Delete all rows from the table
DELETE FROM employees;

In this example, the TRUNCATE statement would remove all rows from the employees table and reset any associated sequences. The DELETE statement would also remove all rows, but it would not reset the sequences.

Deleting All Data from a PostgreSQL Database

If you need to delete all data from all tables in a PostgreSQL database, you can use the pg_truncate_all_tables function. This function is available as an extension in PostgreSQL and provides a convenient way to truncate all tables in a database.

To use the pg_truncate_all_tables function, you first need to install the pg_truncate_all_tables extension. You can do this by running the following command:

CREATE EXTENSION pg_truncate_all_tables;

Once the extension is installed, you can use the pg_truncate_all_tables function to truncate all tables in a database with a single command:

SELECT pg_truncate_all_tables();

This function will truncate all tables in the current database, including any dependent tables, and reset any associated sequences.

Related Article: How to Drop All Tables in a PostgreSQL Database