26

I tried this ..

 select 'drop table if exists "' || tablename || '" cascade;' 
from pg_tables
 where schemaname = 'public';

but doesn't seems to work out for one command?

Cœur
39k25 gold badges206 silver badges282 bronze badges
asked Nov 16, 2013 at 10:53

2 Answers 2

66

If all of your tables are in a single schema, this approach could work (below code assumes that the name of your schema is 'public')

drop schema public cascade;
create schema public;

Drop all tables in PostgreSQL?

see above link for more answers

answered Nov 16, 2013 at 10:58
Sign up to request clarification or add additional context in comments.

5 Comments

ERROR: must be owner of schema public
yes i did , may be syntax is correct but some sort authentication need to be handle .... dono what ?
have you specified owner as anything in the schema property window?
If you're not currently the superuser (postgres) then the last step should also be ALTER SCHEMA public OWNER TO postgres;
Be aware that if you do this, then all users that you created will no longer be able to access the schema, and all queries will fail for that user. You will need to give the user access to the schema: grant usage on schema public to dbuser; You must be the owner of the schema to do this, so also ensure you have followed Jay's advice in the comment above.
11

Run the following bash script:

psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop table \"' || tablename || '\" cascade;' from pg_tables where schemaname='public'" | psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db>

I copied from here: http://www.commandlinefu.com/commands/view/12989/postgresql-drop-all-tables-from-a-schema

It worked for me.

answered Mar 5, 2015 at 0:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.