Lock or disable connections to Postgres while performing maintenance
You can set connection limit for role:

ALTER ROLE x SET CONNECTION LIMIT 0;


or for databases:

ALTER DATABASE db SET CONNECTION LIMIT 0;


After your maintenance chores are done, reset your limit to its previous value such as -1 for unlimited connections.