0

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)'
swiss_knight
  • 5,787
  • 8
  • 50
  • 92
  • 1
    In the second one, surely you have already connected before `create_engine` gets to do anything. So it is too late to set any arguments as you connect. – Richard Huxton Feb 09 '23 at 07:17

0 Answers0