How can I delete all tables in postgresql, working from the command line?
I don't want to drop the database itself - just all tables and all the data in them.
Thanks.
How can I delete all tables in postgresql, working from the command line? I don't want to drop the database itself - just all tables and all the data in them. Thanks. |
|||||||||||||
|
You can use the string_agg function to make a comma-separated list, perfect for DROP TABLE. From a bash script:
|
|||
|
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')
|
|||
You can write a query to generate a SQL script like this:
Or:
In case some tables are automatically dropped due to cascade option in a previous sentence. Additionally, as stated in the comments, you might want to filter the tables you want to drop by schema name:
And then run it. Glorious COPY+PASTE will also work. |
|||||||||||||||||||||
|
should do the trick. |
|||||||||
|
Just in case... Simple Python script that clean Postgresql database
Make sure that after copying it the indentation is right since Python relies on it. |
|||
|
Following Pablo and LenW, here's a one-liner that does it all both preparing and then executing:
NB: either set or replace |
|||
|
If you have the PL/PGSQL procedural language installed you can use the following to remove everything without a shell/Perl external script.
Rather than type this in at the "psql" prompt I would suggest you copy it to a file and then pass the file as input to psql using the "--file" or "-f" options:
Credit where credit is due: I wrote the function, but think the queries (or the first one at least) came from someone on one of the pgsql mailing lists years ago. Don't remember exactly when or which one. |
||||
|
As per Pablo above, to just drop from a specific schema, with respect to case:
|
||||
|