Resetting a PostgreSQL database means wiping existing data (or the whole database) so you can start fresh while keeping or recreating the schema.
In PostgreSQL, there is no single RESET DATABASE statement. "Reset" normally implies either truncating all tables inside the current database or dropping and recreating the database itself.
Run a single TRUNCATE … RESTART IDENTITY CASCADE.It removes data, clears foreign-key dependencies, and restarts serial sequences back to 1.
BEGIN;
TRUNCATE TABLE
"OrderItems",
"Orders",
"Products",
"Customers"
RESTART IDENTITY CASCADE;
COMMIT;
Use DROP DATABASE followed by CREATE DATABASE.You need superuser or the database owner privileges and must connect to a different database (usually postgres) before dropping.
-- From psql connected to the maintenance DB
dropdb myshop;
createdb myshop -T template0;
If you want a clean schema without disconnecting users, drop and recreate the public schema.
BEGIN;
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO public;
COMMIT;
Choose TRUNCATE when you only need to empty data during tests.Choose DROP DATABASE when you must change encoding, collation, or fully wipe everything quickly.
Combine TRUNCATE with sequence resets inside a transaction. This keeps constraints valid and avoids temporary inconsistencies.
BEGIN;
TRUNCATE TABLE "OrderItems", "Orders", "Products", "Customers" RESTART IDENTITY CASCADE;
COMMIT;
1) Always back up first with pg_dump. 2) Disconnect application sessions before DROP DATABASE.3) Use explicit table lists; wildcards don’t work in TRUNCATE.
TRUNCATE keeps structure intact, so indexes, constraints, and permissions stay.DROP DATABASE removes everything, including ownership grants.
Instead of truncating, delete rows with WHERE customer_id = … and then reset relevant sequences via ALTER SEQUENCE.
DELETE FROM "Orders" WHERE customer_id = 42;
SELECT setval(pg_get_serial_sequence('Orders','id'), 1, false);
Use TRUNCATE … RESTART IDENTITY for quick data wipes. Use DROP DATABASE or DROP SCHEMA for full resets.Always confirm you are connected to the right database before running destructive commands.
.
Yes. TRUNCATE is metadata-only, bypassing individual row deletes and WAL logging, making it much faster on large tables.
When run inside a transaction block, TRUNCATE can be rolled back like any other DML statement.
Yes. If the extension objects reside in public, they are removed. Re-install the extension or place it in a dedicated schema.