PostgreSQL Error: database is being accessed by other users

In this article, we will the solution for PostgreSQL Error: database is being accessed by other users
Detail: There are 2 other sessions using the database

ERROR  database  is being accessed by other users

SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'database_name' AND pid <> pg_backend_pid();

Example: If we want to kill sessions for the database ‘dvdrental’ then run the below query.

PostgreSQL Error database is being accessed by other users