Postgresql: how to get all queries to a tables or database for a current user? - postgresql

I need to get all queries to a tables for current user in Postgresql, how to do that? I need it for an audit. I installed pgbadger but it was not show queries its just showing a count of some queries,for example, user1 have made 3 queries to a table1, but i need to get something like this: “user1 made select * from table1 at %timestamp%; user1 made alter table … “ and etc. could someone help?

i used pgbadger and try this solution from postgres off docs
in-build tool for monitor user activity

Related

PostgreSQL query to list all table names from all servers

I have 13 servers in Pgadmin, and about 100 databases in each
i know what it possible fro psql SELECT datname FROM pg_database; but it need 13 times do that.
I want to export list of all server at all tables names on each. How it possible do that in one query? something like sp_MSForEachDB
How to get also list of databases which is in use, and check when was last access time on each database, as need to delete not used databases?

Move table to different schema in postgres

I would like to change schema of few tables in my Postgres DB. Problem is that all the time there are long running queries and as I understand schema change needs exclusive lock.
Question is how can I do it? Of course I can kill all existing queries and try to do schema rename (move table to different schema) but there is a huge chance that in the meantime new queries will appear.
Thanks for help!
run SELECT pg_backend_pid() before running the ALTER TABLE
start the ALTER TABLE statement
in a second database session, run SELECT pg_blocking_pids(12345), where 12345 is the result from the first query
cancel all the blocking transactions found with the previous query with SELECT pg_cancel_backend(23456)

postgres db trigger to log query type into another table

This problem scenario may sounds strange but I am trying to write a trigger to log the query type into another table and so far i havent been able to find anything on google
the database i am using is postgres
i.e.
if i have two tables; table1 and querylog(has a string field called querytype)
and a select query is executed on table1, i want to insert a row into the query log table with the querytype field populated with "select"
anyone have any idea how to reference the query type in a function that will be called by a trigger?
Triggers do not get called for SELECT queries, so that won't work.
If you want to audit queries, you can use the PostgreSQL log file or tools like pgaudit that hook into PostgreSQL to retrieve and log the information.

DB2 table access list

i want to find db2 tables access list(which user or program has privilige which table). how can i query this?
If i would write this psedue code i wil be like this.
select table's_grant_user_name from sysibm.... where table_name='XXX'`
is there any ibm privillige table been in db2?
In aqt tool i can see tables access list when i select from combobox. But i need this query to querying for some tables to groupping.
is it possible to query this ? how can i retrieve table's grant list?
Thanks,
Check out the view SYSIBMADM.PRIVILEGES.

Postgres: Is there a query to return the tables owned by a given user?

I need a query (not a command like \dn or \dt or whatnot as I am not running this from the command line) that will let me specify a username, and get all of the tables associated with that user (and ideally how much disk space it is using and other details).
Does such a command exist? Thank you very much.
This will provide the table name, and its owner as well as the userid
SELECT pgc.relname, pgc.relowner, pgs.usename
FROM pg_class pgc JOIN pg_shadow pgs
ON pgc.relowner = pgs.usesysid;
You can look up such information in the catalog. The catalog is a collection of tables containing meta data about a running Postgres instance. Therefore it is easy to query with SQL. To find all tables owned by a particular user you can query pg_tables. For sizes you probably have to join pg_class.