How can you connect active users to postgreSQL database via SQL?

How can you connect active users to postgreSQL database via SQL? This can be a user ID or the number of users.

+78
sql postgresql
Jan 21 '09 at 9:46
source share
3 answers

(question) Do not get this information in

select * from pg_user ;

or using the pg_stat_activity view :

select * from pg_stat_activity; 

Added:

the submission says:

One line per server process, showing the database identifier, database name, process identifier, user identifier, username , current request, request wait status, start time of the current request, the time it was started, and the client address and port number . Columns that report current request data are available if stats_command_string is not disabled. In addition, these columns are only visible if the user viewing the view is the superuser or the same as the user owning the process being reported.

You cannot filter and receive this information? which will be current users in the database, you can use the started runtime to get all requests in the last 5 minutes, for example ...

something like that.

+103
Jan 21 '09 at 9:50
source share

Using balexandre information:

 SELECT usesysid, usename FROM pg_stat_activity; 
+38
Jan 21 '09 at 9:58
source share

The OP requested users connected to a specific database:

 -- Who currently connected to my_great_database? SELECT * FROM pg_stat_activity WHERE datname = 'my_great_database'; 

This gives you all sorts of juicy information (as others have already mentioned), such as

  • user id ( usesysid column)
  • Username ( usename )
  • the name of the client application ( appname ), if he does not want to set this variable - psql does :-)
  • IP Address ( client_addr )
  • what state is it (a pair of columns related to the state and wait status)
  • and all your favorites, the current SQL command is executed ( query )
0
Jul 18 '19 at 5:54
source share



All Articles