[postgresql] How can I drop all the tables in a PostgreSQL database?

well, since I like working from the command line...

psql -U <user> -d <mydb> -c '\dt' | cut -d ' ' -f 4 | sed -e "s/^/drop table if exists /" | sed -e "s/$/;/"

-c '\dt' will invoke the list tables command.

List of relations Schema | Name | Type | Owner --------+-------------------+-------+---------- public | _d_psidxddlparm | table | djuser public | _d_psindexdefn | table | djuser

cut -d ' ' -f 4 now, pipe its output to grab the 4th field (when using space as separator), which is the table.

sed is then used to prefix a drop table and suffix the ; command separator.

| egrep '_d_' - Pipe it into grep some more and you can be more selective about which tables you drop.

drop table if exists _d_psidxddlparm; drop table if exists _d_psindexdefn;

Note: as written, this will generate bogus rows for the \dt commands output of column headers and total rows at the end. I avoid that by grepping, but you could use head and tail.