When debugging programs, I need to frequently drop a database of Postgresql, and then re-create it again.

To convenient, I want to execute this in a single command line like following:

psql -c "DROP DATABASE my_db; CREATE DATABASE my_db;"

But I get a error:

ERROR: DROP DATABASE cannot run inside a transaction block

Is there a way I can do it? Thanks!

1 Answer 1


You can use the -c option repeatedly:

psql -c 'DROP DATABASE my_db' -c 'CREATE DATABASE my_db'
  • Thank you very much!
    – Leon
    Oct 10, 2022 at 14:25

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.