Sending postgres querys to a middleware and edit it

What I want to do:

  1. Grafana sends query to my middleware: “select * from peter”.

  2. The middleware swaps names for id: “select * from id123” and sends the query to the actual postgres database.

  3. The data received by the postgresdb will be sent back to grafana.

How can I accomplish this? Which protocol has to be used?