Connecting to postgresql database using an external tool like (IDE, SQL client) using brokered credentials

We have a usecase where a developer or data scientist needs to connect to a postgresql instance using a IDE or database tool and not the psql client.
Using the postgres connector with boundary connect postgres -target-id $ANALYST_TARGET_ID -dbname northwind is nice for executing simple SQL command on the CLI but is a bit cumbersome for doing more complex tasks.

I understand that with using -exec option I can intercept the command to run when connecting.
I did a simple shell script that blocks for a while and shows some of the setup that the connector provides.

env
cat $PGPASSFILE
sleep 1000

There are several BOUNDARY_* env variables exposing the tcp connection parameters.
I can get hold of the password that has been written to a temp pgpass file.

In case it was possible to find the username as well it would be possible to use that to configure any database client.

So my question is: How can I get the username that is active during the session?

Try using the -format json flag which will show the raw output of the response in JSON format including the brokered credentials.