0

Whereas How to drop a table based on IF condition in postgres? solves the problem of conditionally dropping a table, it doesn't work for dropping a database because:

ERROR:  DROP DATABASE cannot be executed from a function

I want to drop a database but only if it is has no user tables. How can I execute any kind of IF if I can't do it from function?

2
  • 1
    The function issue is a subset of DROP DATABASE not being allowed in a transaction. Functions run in a transaction, hence the error. This is going to be something you need to do from outside Postgres using a script. Commented May 16, 2021 at 17:15
  • Ah. So the best I can do is to move the IF control out of the postgres script into the hosting shell script; and then I can ask psql to drop database unconditionally Commented May 17, 2021 at 8:21

1 Answer 1

0

You'll first have to connect to the database, count the tables, connect to a different database (for example postgres), revoke connections to the database from everybody, kill all database sessions and run DROP TABLE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.