I have been using pgAdmin 4 as my GUI tool for interacting with my PostgreSQL database servers. I had noticed the “Application” column, and that it was empty for my Django sessions, and wanted to fill that in with useful context..
With a little research I found that there were two simple ways of setting the application name. One was with a statement: SET application_name = 'test_activity';
This is simple enough, but Django does not provide an easy way to add this. With MySQL/MariaDB, we could have used the init_command
dictionary key of OPTIONS
.
Another way to handle this is by setting the application_name
argument of the psycopg2.connect
function. Reading through the Django code, I see that all dictionary items of OPTIONS
are passed as keyword arguments to psycopg2.connect
. So setting the application name is as simple as adding an item to the OPTIONS
with the key application_name
.
In my case, I use some if-statements based on sys.argv
to come up with a meaningful value for application name. And now, my pgAdmin4 happily shows me my application name. Now I can easier differentiate between a connection from Guincorn, a Celery task, or a management command.