I’m searching for a simple way to delete all data from a database and keep the structure (table, relationship, etc…).
I using postgreSQL but I think, if there a command to do that, it’s not specific to postgres.
Thanks,
Damien
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Dump the schema using
pg_dump. drop the database, recreate it and load the schema.Dump you database schema (the -s tag) to a file:
Delete the database:
Recreate it:
Restore the schema only:
This should work on PostgreSQL; Other DBMS might have their own tools for that. I do no know of any generic tool to do it.
EDIT:
Following comments, you might want to skip the
dropdbcommand, and simply create another database with the dumped schema. If all went through well, you can drop the old database:At this point, you have the full database at DB-NAME, and an empty schema at DB-NEW-NAME. after you’re sure everything is OK, use
dropdb DB-NAME.