0

I need to create and delete tables in a test database while in development stage, but foreign keys verification bores me every time I need to erase its tables.

Is there any form to bypass this in Postgresql?

2
  • Do you delete rows? Or do you drop the tables? It's unclear to me based on your question
    – user1822
    Commented Nov 7, 2015 at 19:52
  • Hi @a_horse_with_no_name I hadn't seen your comment. I'm sorry if I was not clear. But I've solved. Thank you for your disposition. Commented Nov 7, 2015 at 19:57

1 Answer 1

8

I've got it.

For a single table I can do:

DROP TABLE "tableName" CASCADE

For all tables inside a schema, for example "public", I can do:

DROP SCHEMA public CASCADE;
CREATE SCHEMA public;

Source:

http://www.postgresql.org/docs/current/static/sql-droptable.html

https://stackoverflow.com/a/13823560/937110

Or, as suggested by a_horse_with_no_name:

DROP OWNED BY name [, ...] [ CASCADE | RESTRICT ]

source: http://postgresql.org/docs/current/static/sql-drop-owned.html

4
  • 1
    There is also drop owned by that drop all objects owned by a user. Are you really still using Postgres 8.2?
    – user1822
    Commented Nov 7, 2015 at 20:32
  • I'm using PostgreSQL: 9.4.4 through PHPpgsql under Vagrant. I'm creating tables for testing and then I erase them. If you have a better solution I'm listening you. Commented Nov 7, 2015 at 21:06
  • Not necessarily better, but an alternative: postgresql.org/docs/current/static/sql-drop-owned.html
    – user1822
    Commented Nov 7, 2015 at 21:11
  • It's a good alternative. Thank you @a_horse_with_no_name. I think I'll use this. Commented Nov 7, 2015 at 21:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.