Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you get the active users connected to a postgreSQL database via SQL?

How can you get the active users connected to a postgreSQL database via SQL? This could be the userid's or number of users.

like image 227
Johan Bresler Avatar asked Jan 21 '09 09:01

Johan Bresler


People also ask

How do I see active connections in PostgreSQL?

SELECT * FROM pg_stat_activity WHERE datname = 'dbname' and state = 'active'; Since pg_stat_activity contains connection statistics of all databases having any state, either idle or active , database name and connection state should be included in the query to get the desired output. Show activity on this post.

How do I see all users in PostgreSQL?

Use \du or \du+ psql command to list all users in the current database server. Use the SELECT statement to query the user information from the pg_catalog. pg_user catalog.

How do I find out how many users are connected to my database?

Have a look at running sp_who or sp_who2 in Query Analyzer. It will show you the databases users are connected to. Each unique SPID is a user connection.


2 Answers

(question) Don't you get that info in

select * from pg_user;

or using the view pg_stat_activity:

select * from pg_stat_activity; 

Added:

the view says:

One row per server process, showing database OID, database name, process ID, user OID, user name, current query, query's waiting status, time at which the current query began execution, time at which the process was started, and client's address and port number. The columns that report data on the current query are available unless the parameter stats_command_string has been turned off. Furthermore, these columns are only visible if the user examining the view is a superuser or the same as the user owning the process being reported on.

can't you filter and get that information? that will be the current users on the Database, you can use began execution time to get all queries from last 5 minutes for example...

something like that.

like image 119
balexandre Avatar answered Sep 20 '22 08:09

balexandre


Using balexandre's info:

SELECT usesysid, usename FROM pg_stat_activity; 
like image 44
Sven Lilienthal Avatar answered Sep 21 '22 08:09

Sven Lilienthal