Last Updated: February 25, 2016
·
1.386K
· jfragoulis

Recreate schema in PostgreSql

I was using dropdb and createdb to recreate a whole database when I needed to do a complete clean up for my under development project.

I came across a situation where I was not able to connect using the (default) maintenance database (which is template1 in 9.2, for more read here). So to make drop/create work without the maintenance database I had to improvise. I connected to the database I wanted to drop, and instead I dropped and recreated the public schema instead.

set PGPASSWORD=password psql --host=host --username=username --command="drop schema public cascade;create schema public;" database

The set PGPASSWORD=password part is a hack for running psql commands in a script without having to enter the password for every command or modifying any global settings for postgres.