Drop database from remote that has connected users
First, you can issue DROP DATABASE; from a multiline script, but it has to be the only statement of its transaction:

CREATE DATABASE droptest;

\c droptest

CREATE TABLE a (id integer);


Then run the script:

SELECT id
FROM a;

\c postgres

DROP DATABASE droptest;


It results in: 

 id 
----
(0 rows)

psql (9.1.9, server 9.0.6)
WARNING: psql version 9.1, server version 9.0.
         Some psql features might not work.
You are now connected to database "postgres" as user "postgres".
DROP DATABASE


And the documentation says:


  DROP DATABASE cannot be executed inside a transaction block.


This means it can't be run like psql -1 -f the_above_script.

So, now my solution (hopefully):

First disable connections on the DB:

REVOKE CONNECT ON database_name FROM ALL;


Then do what you do now:

SELECT pg_terminate_backend(procpid) 
FROM pg_stat_activity 
WHERE 
    datname = '$DB_NAME' and 
    current_query NOT LIKE '%SELECT pg_terminate_backend(procpid)%'
;

DROP DATABASE database_name;