in Postgresql

Removing and recreating all tables in a Postgresql database

One of the things I do on a regular basis is validating database backups. This requires getting a copy of the database that was dumped out to a sql file and loading back into a database to be checked.

Sometimes, depending on the format of the dump file, the database complains about tables already existing and duplicate keys and other such errors. There are flags and parameters that can be added to stop from adding ids and overwriting existing tables but I found the simplest way for me was to remove the existing tables from the database and recreate the structure with no content.

Here is the SQL query that quickly drops and recreates the database schema.

DROP SCHEMA public CASCADE; CREATE SCHEMA public;

 

Write a Comment

Comment