O'Reilly logo

PostgreSQL 9 Administration Cookbook by Hannu Krosing, Simon Riggs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Pushing users off the system

Sometimes we may need to remove groups of users from the database server for various operational reasons. Here's how.

How to do it...

You can terminate a user's session with the pg_terminate_backend() function included with PostgreSQL. That function takes the pid, or the process ID, of the user's session on the server. This is known as the backend, and is a different system process from the program that runs the client.

To find out the pid of a user, we can look at the view pg_stat_activity. We can use that in a query like the following:

SELECT pg_terminate_backend(procpid)
FROM		 pg_stat_activity
WHERE ...

There's a couple of things to note if you run that query, which are as follows:

  • If the WHERE clause matches no sessions, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required