How to do it…

Connections can be prevented in a number of ways, as follows:

ALTER DATABASE foo_db CONNECTION LIMIT 0;
ALTER USER foo CONNECTION LIMIT 0;
# TYPE   DATABASE  USER         ADDRESS    METHOD
local all all reject
host all all 0.0.0.0/0 reject

If you still want superuser access, then try something like the following:

# TYPE   DATABASE    USER         ADDRESS         METHOD
local all postgres peer
local all all reject
host all all 0.0.0.0/0 reject

This will prevent connections to the database by any user except the postgres operating system user ID, which connects locally to any database. Be careful not to confuse the second and third columns—the second column is the database and the third column is the username. It's worth keeping the header line just for that reason. The peer method should be replaced by other authentication methods if a more complex configuration is in use.