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

Is the user connected?

Here we show how to learn if a certain database user is currently connected to database.

Getting ready

Make sure that you are logged in as a superuser.

How to do it...

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

SELECT datname FROM pg_stat_activity WHERE usename = 'bob';

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

How it works...

PostgreSQL system view pg_stat_activity keeps a track of all running PostgreSQL backends, including what queries are running, who is connected and when they were connected, and when the current transaction and current query were started.

There's more...

There is more information ...

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