This is correctly printing something in the application_name
column of the pg_stat_activity
table in my PostgreSQL 14 database:
import psycopg2
from sqlalchemy import create_engine
engine = create_engine(
'postgresql+psycopg2://john:doe@localhost:5432/mydatabase',
connect_args={
"application_name": 'my_app'
})
Whereas this is not printing anything (but data can be read/written to the DB):
import psycopg2
from sqlalchemy import create_engine
def get_dbapi_connection():
res = psycopg2.connect(**{
'user': 'john',
'password': 'doe',
'host': 'localhost',
'port': '5432',
'dbname': 'mydatabase',
})
return res
engine = create_engine(
'postgresql+psycopg2://',
creator=get_dbapi_connection,
connect_args={
"application_name": 'my_app'
})
Why? And how to fix the second code snippet?
I cannot find anything useful in the doc.
And this thread is not helping much as it doesn't use a creator
.
Version info:
- Python '3.9.7'
- SQLAlchemy '2.0.1'
- psycopg2 '2.9.5 (dt dec pq3 ext lo64)'