postgresql – To Kill postgresql Sessions for a specific database
Sometimes it is necessary for the database administrator to remove existing connected sessions. For example to drop a database when users and/or applications cannot (or will not) disconnect or terminate cleanly.
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'database_name_with_existing_sessions' AND pid <> pg_backend_pid();