Find the PID of the query from a system utility like top or htop, or from the “pg_catalog.pg_stat_activity” table.

sudo -u postgres psql 
postgres=# SELECT pg_cancel_backend(28710);