The first trivial solution is to switch the database to single user mode. It can be done by the following command:
And switch back to multi user mode:
It is also possible to kill the database connections. In this case clients may connect or reconnect again after the script.