Book Image

PostgreSQL 9 Administration Cookbook - Second Edition

Book Image

PostgreSQL 9 Administration Cookbook - Second Edition

Overview of this book

Table of Contents (19 chapters)
PostgreSQL 9 Administration Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Checking whether a user is connected


Here, we will show you how to learn whether a certain database user is currently connected to the database.

Getting ready

Make sure that you are logged in as a superuser.

How to do it…

Issue the following query to see whether the bob user is connected:

SELECT datname FROM pg_stat_activity WHERE usename = 'bob';

If this query returns any rows, then it means bob is connected to the database. The returned value is the name of the database to which the user is connected.

How it works…

PostgreSQL's pg_stat_activity system view keeps track of all running PostgreSQL backends. This includes information such as the query that is being currently executed (or the last query that was executed by a backend—available from 9.2); who is connected; when the connection, the transaction, and/or the query were started; and so on.

There's more…

If you've managed different versions of PostgreSQL, you may be aware that the pg_stat_activity view has undergone some important changes...