How to Reset a Database in PostgreSQL

Galaxy Glossary

How do I reset a PostgreSQL database without losing the schema?

Resetting a PostgreSQL database means wiping existing data (or the whole database) so you can start fresh while keeping or recreating the schema.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

What does “resetting” a PostgreSQL database really mean?

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.

How do you wipe all rows but keep the tables and sequences?

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;

How do you drop and recreate the entire database?

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;

Can you reset by recreating the public schema only?

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;

Which method should I choose?

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.

How do you script a test-environment reset?

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;

Best practices for resetting safely?

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.

What happens to indexes, constraints, and grants?

TRUNCATE keeps structure intact, so indexes, constraints, and permissions stay.DROP DATABASE removes everything, including ownership grants.

How do you reset only a specific tenant’s data?

Instead of truncating, delete rows with WHERE customer_id = … and then reset relevant sequences via ALTER SEQUENCE.

Example: reset one customer

DELETE FROM "Orders" WHERE customer_id = 42;
SELECT setval(pg_get_serial_sequence('Orders','id'), 1, false);

Key takeaways

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.

.

Why How to Reset a Database in PostgreSQL is important

How to Reset a Database in PostgreSQL Example Usage


-- Reset an e-commerce staging DB without dropping it
BEGIN;
TRUNCATE TABLE "OrderItems", "Orders", "Products", "Customers"
RESTART IDENTITY CASCADE;
COMMIT;

How to Reset a Database in PostgreSQL Syntax


-- Empty all tables but keep schema
TRUNCATE TABLE table1 [, table2 …]
    [ RESTART IDENTITY | CONTINUE IDENTITY ]
    [ CASCADE | RESTRICT ];

-- Full database recreation from psql or shell
dropdb [--if-exists] dbname
createdb dbname [-T template]

-- Public schema reset inside a DB
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;

Common Mistakes

Frequently Asked Questions (FAQs)

Is TRUNCATE faster than DELETE?

Yes. TRUNCATE is metadata-only, bypassing individual row deletes and WAL logging, making it much faster on large tables.

Can I rollback a TRUNCATE?

When run inside a transaction block, TRUNCATE can be rolled back like any other DML statement.

Does DROP SCHEMA affect extensions?

Yes. If the extension objects reside in public, they are removed. Re-install the extension or place it in a dedicated schema.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.