Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log SQL queries on a Google Cloud SQL PostgreSQL 11 instance?

I have to log all DDL and DML queries executed on a Google Cloud SQL PostgreSQL instance.

I checked a lot of websites, but there is no clear information available. I tried using the pgAudit extension, but that is not supported by Cloud SQL.

Can someone please suggest the extension to be used or any other way of logging SQL queries? Also, if the user logins can be logged, then that will be helpful, too.

like image 878
Vaishnavi Killekar Avatar asked Jul 16 '26 18:07

Vaishnavi Killekar


2 Answers

Short Answer - Database Flags

The solution provided in the other answer can be used, if PostgreSQL is locally installed or if we have access to the server container. In Google Cloud, however, this file cannot be directly accessed from the instance.

I found that this can be achieved on Google Cloud SQL instance by setting the various parameters given in this link - PostgreSQL configuration parameters as database flags.

Note: All of the parameters are not supported, hence verify in the official documentation by Google given below.

Google Cloud Database Flags for PostgreSQL

like image 63
Vaishnavi Killekar Avatar answered Jul 20 '26 16:07

Vaishnavi Killekar


Add in postgresql.conf:

log_statement=mod

https://www.postgresql.org/docs/12/runtime-config-logging.html says

logs all ddl statements, plus data-modifying statements such as INSERT, UPDATE, DELETE, TRUNCATE, and COPY FROM. PREPARE, EXECUTE, and EXPLAIN ANALYZE statements are also logged if their contained command is of an appropriate type.

To log connections and disconnections, add in postgresql.conf:

log_connections=on
log_disconnections=on
like image 40
pifor Avatar answered Jul 20 '26 16:07

pifor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!