I have a cron job that runs a script that connects to a remote postgres database that I do not administer. We are allotted a single connection and, on the occasion that I or a colleague log in to run some manual queries and forget to log out, my script will fail
FATAL: too many connections for role
I need to run some command within either crontab or the script itself to close any connections for my user before attempting to run my script.
select pg_cancel_backend(pid) from pg_stat_activity where pid <> pg_backend_pid();
isn't helpful because to do so, my script would have to be able to make the connection.
Any option that doesn't require being logged in already or being an administrator of the db should work as the script should take priority over anything and the only users of this role are users that should defer to the script running on time.