To generate a script in postgreSQL, equivalent to the Oracle-style shown below:
select 'truncate table ' || table_name || ' cascade;' from dba_tables where owner='MYSCHEMA';
, put this in a file called gen_truncate.sql
/*
| Setting | Effect |
| ------------------------ | --------------------------------------------------------- |
| `\o /path/to/file` | Redirects all query output to the file |
| `\pset format unaligned` | Produces plain text output (no table formatting) |
| `\pset tuples_only on` | Suppresses headers and row counts |
| `\pset footer off` | Removes `x rows` footer |
| `\pset border 0` | Removes any border formatting (mostly for aligned format) |
*/
\o truncate_tables.sql
\pset format unaligned
\pset tuples_only on
\pset footer off
\pset border 0
SELECT 'TRUNCATE TABLE ' || schemaname || '.' || tablename || ' CASCADE;' AS stmt
FROM pg_tables
WHERE schemaname = 'myschema';
\o
Execute it:
psql mydb -f gen_truncate.sql
or login to the desired database directly as the owner of the table:
psql -h localhost -d mydb -U myuser -f gen_truncate.sql
No comments:
Post a Comment