Closed arafat-java closed 3 years ago
Easiest thing one can do is to configure Postgres to log statements to stdout. I use this script:
#!/bin/sh
set -x
PGCONF=/var/lib/postgresql/data/postgresql.conf
sed -i "s/#log_statement = 'none'.*/log_statement = 'all'/g" $PGCONF
I've been using AWS RDS so that'll be a bit difficult for me
There is also go-pg
query hook for this purpose:
type dbLogger struct{}
func (d dbLogger) BeforeQuery(ctx context.Context, q *pg.QueryEvent) (context.Context, error) {
fmt.Println(q.FormattedQuery())
return ctx, nil
}
func (d dbLogger) AfterQuery(ctx context.Context, q *pg.QueryEvent) error {
return nil
}
db := pg.Connect(opts)
db.AddQueryHook(dbLogger{})
Thanks @pckhoi Works perfectly for me
Closing this out
We are using casbin-pg-adapter for our policies storage to DB I would like to log all the queries that are fired to the DB. How can I do that?