You can use the application_name (optional) parameter which is offered by libpq
import psycopg2 as psp
stuff = {
'dbname': 'twitters',
# 'host': '127.0.0.1',
'user': 'twitwww',
'password': 'secret',
'application_name': 'myap-1.0' }
conn = psp.connect(**stuff) # use kwargs
print(conn)
curs = conn.cursor()
print(curs)
curs.execute("select * from pg_stat_activity where datname = 'twitters' ")
for row in curs:
print ''
print (row)
print ''
conn.close()
Result:
$ python psychopg.py
<connection object at 0x7f40e391a7c0; dsn: 'user=twitwww password=xxxxxxxxxxxx application_name=myap-1.0 dbname=twitters', closed: 0>
<cursor object at 0x7f40e38ff528; closed: 0>
(20529, 'twitters', 2586, 10, 'postgres', 'pgAdmin III - Browser', None, None, None, None, None, None, None, None, None, None, None, None, '<insufficient privilege>', None)
(20529, 'twitters', 25763, 5223264, 'twitwww', 'myap-1.0', None, None, -1, datetime.datetime(2020, 10, 17, 14, 3, 10, 946424, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=120, name=None)), datetime.datetime(2020, 10, 17, 14, 3, 10, 948886, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=120, name=None)), datetime.datetime(2020, 10, 17, 14, 3, 10, 949015, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=120, name=None)), datetime.datetime(2020, 10, 17, 14, 3, 10, 949015, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=120, name=None)), None, None, 'active', None, '119920335', "select * from pg_stat_activity where datname = 'twitters' ", 'client backend')