From PostgreSQL 13, you can force a database drop using the WITH(FORCE) option, which will not raise an error if the database is being used.
WITH(FORCE)
Complete command example:
DROP DATABASE my_db WITH(FORCE);