Hello I want to delete all data in my postgresql tables, but not the table itself. How could I do this?
-
1possible duplicate of [Truncating all tables in a postgres database](http://stackoverflow.com/questions/2829158/truncating-all-tables-in-a-postgres-database) – Craig Ringer Nov 04 '12 at 23:02
5 Answers
Use the TRUNCATE TABLE
command.

- 2,901
- 3
- 30
- 47

- 951,095
- 183
- 1,149
- 1,285
-
8Thanks! Thats it! With this information i found this: http://stackoverflow.com/questions/2829158/truncating-all-tables-in-a-postgres-database – vo1d Nov 04 '12 at 22:59
The content of the table/tables in PostgreSQL database can be deleted in several ways.
Deleting table content using sql:
Deleting content of one table:
TRUNCATE table_name;
DELETE FROM table_name;
Deleting content of all named tables:
TRUNCATE table_a, table_b, …, table_z;
Deleting content of named tables and tables that reference to them (I will explain it in more details later in this answer):
TRUNCATE table_a, table_b CASCADE;
Deleting table content using pgAdmin:
Deleting content of one table:
Right click on the table -> Truncate
Deleting content of table and tables that reference to it:
Right click on the table -> Truncate Cascaded
Difference between delete and truncate:
From the documentation:
DELETE deletes rows that satisfy the WHERE clause from the specified table. If the WHERE clause is absent, the effect is to delete all rows in the table. http://www.postgresql.org/docs/9.3/static/sql-delete.html
TRUNCATE is a PostgreSQL extension that provides a faster mechanism to remove all rows from a table. TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE on each table, but since it does not actually scan the tables it is faster. Furthermore, it reclaims disk space immediately, rather than requiring a subsequent VACUUM operation. This is most useful on large tables. http://www.postgresql.org/docs/9.1/static/sql-truncate.html
Working with table that is referenced from other table:
When you have database that has more than one table the tables have probably relationship. As an example there are three tables:
create table customers (
customer_id int not null,
name varchar(20),
surname varchar(30),
constraint pk_customer primary key (customer_id)
);
create table orders (
order_id int not null,
number int not null,
customer_id int not null,
constraint pk_order primary key (order_id),
constraint fk_customer foreign key (customer_id) references customers(customer_id)
);
create table loyalty_cards (
card_id int not null,
card_number varchar(10) not null,
customer_id int not null,
constraint pk_card primary key (card_id),
constraint fk_customer foreign key (customer_id) references customers(customer_id)
);
And some prepared data for these tables:
insert into customers values (1, 'John', 'Smith');
insert into orders values
(10, 1000, 1),
(11, 1009, 1),
(12, 1010, 1);
insert into loyalty_cards values (100, 'A123456789', 1);
Table orders references table customers and table loyalty_cards references table customers. When you try to TRUNCATE / DELETE FROM the table that is referenced by other table/s (the other table/s has foreign key constraint to the named table) you get an error. To delete content from all three tables you have to name all these tables (the order is not important)
TRUNCATE customers, loyalty_cards, orders;
or just the table that is referenced with CASCADE key word (you can name more tables than just one)
TRUNCATE customers CASCADE;
The same applies for pgAdmin. Right click on customers table and choose Truncate Cascaded.

- 9,781
- 6
- 29
- 30
-
`TRUNCATE` is part of ANSI SQL, and supported in _all_ DBMS. I followed the link, and the document mentions nothing about extensions. Perhaps the link is incorrect or out of date? – Manngo Apr 11 '18 at 02:21
-
Hm, interesting. The cited text still could be found here: https://www.postgresql.org/docs/9.0/static/sql-delete.html but you are right - it is not in doc for 9.1. – vitfo Apr 11 '18 at 07:06
For small tables DELETE
is often faster and needs less aggressive locking (important for concurrent load):
DELETE FROM tbl;
With no WHERE
condition.
For medium or bigger tables, go with TRUNCATE
, like @Greg posted:
TRUNCATE tbl;
Hard to pin down the line between "small" and "big", as that depends on many variables. You'll have to test in your installation.

- 605,456
- 145
- 1,078
- 1,228
-
7
-
4@Jackson: That's hard to pin down exactly because it depends on too many variables. You can run some tests to find the sweet spot on your system. – Erwin Brandstetter May 25 '16 at 04:26
I found a very easy and fast way for everyone who might use a tool like DBeaver:
You just need to select all the tables that you want to truncate (SHIFT + click
or CTRL + click
) then right click
And if you have foreign keys, select also CASCADE
option on Settings
panel. Start
and that's all it takes!

- 184
- 1
- 4
Truncate all tables present in a PostgreSQL database
SET session_replication_role = 'replica';
SELECT tablename FROM pg_tables WHERE schemaname = 'schemaname';
-- Generate and execute the truncate command for each table
DO $$ DECLARE
table_name TEXT;
BEGIN
FOR table_name IN (SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'schemaname') LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(table_name) || ' CASCADE;';
END LOOP;
END $$;
SET session_replication_role = 'origin';
COMMIT;

- 1,853
- 16
- 25