13

I am using pg-promise package with Nodejs to execute PostgreSQL queries. I want to see the queries executed. Only specific queries, say, just one query that I want to debug.

I can see that one recommended way is to use the pg-monitor to catch the events and log them as mentioned here in the examples documentation.

Without using pg-monitor, is there a simple way to just print the prepared query that is executed. I can't see it in the docs.

Example:

db.query("SELECT * FROM table WHERE id = $/id/", {id: 2})

How to print this query to yield?

SELECT * FROM table WHERE id = 2
mythicalcoder
  • 3,143
  • 1
  • 32
  • 42

1 Answers1

26

is there a simple way to just print the prepared query that is executed...

A query in general - yes, see below. A Prepared Query - no, those are by definition formatted on the server-side.

const query = pgp.as.format('SELECT * FROM table WHERE id = $/id/', {id: 2});
console.log(query);
await db.any(query);

And if you want to print all queries executed by your module, without using pg-monitor, simply add event query handler when initializing the library:

const initOptions = {
  query(e) {
    console.log(e.query);
  }
};
const pgp = require('pg-promise')(initOptions);
vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • The `options` is awesome. I am yet to explore it fully. Thanks. – mythicalcoder May 04 '17 at 13:48
  • not sure this question might be related to options - for quicker visibility commenting here. Pl check. Thanks - https://stackoverflow.com/questions/65152018/pg-promise-timeout-while-using-express-generator-format-but-works-fine-with-bas – mythicalcoder Dec 05 '20 at 01:54
  • 1
    when I see a library with too many open issues I often go and tell them "maybe you should manage a library like vitaly-t" first – PirateApp May 09 '22 at 14:35