HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to log all SQL queries for a single connection or user or client host?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
logallsqluserclientsinglehostforhowqueries

Problem

Is there a way to log all SQL queries in Postgres 9.3.9 for a single connection or user or client host?

In other words, I want log_statement = all but only for a certain username or client hostname or client ip or connection id.

One option is to log SQL activity for all clients and search the logs. I'm hoping there's a better way.

Solution

I don't know how to do it for a client host, but for a single database user, you can use this (as a superuser):

ALTER ROLE someone SET log_statement TO 'all';


Which will then turn on log_statement every time that that someone connects in the future.

For a single connection which is cooperating with you, you could create this function (as a superuser):

create or replace function set_log_statement() returns void as $
 set log_statement='all' 
$ language sql security definer ;


And then have the desired single connection invoke this function to turn on logging for itself.

Code Snippets

ALTER ROLE someone SET log_statement TO 'all';
create or replace function set_log_statement() returns void as $$
 set log_statement='all' 
$$ language sql security definer ;

Context

StackExchange Database Administrators Q#116154, answer score: 8

Revisions (0)

No revisions yet.