<@ULVA73B9P> how can I set the application name on...
# marvin-ai
j
@Marvin how can I set the application name on the SQL connection for postgres?
m
thought for 167.6 seconds
Great question! In Prefect 3.x (with the prefect-sqlalchemy integration), you can set the PostgreSQL
application_name
in two ways: 1) Add it as a URL query parameter (simple/static values)
Copy code
python
from prefect_sqlalchemy import SqlAlchemyConnector, ConnectionComponents, SyncDriver

connector = SqlAlchemyConnector(
    connection_info=ConnectionComponents(
        driver=SyncDriver.POSTGRESQL_PSYCOPG2,
        username="user",
        password="password",
        host="localhost",
        port=5432,
        database="mydb",
        query={"application_name": "my_prefect_flow"}  # <- here
    )
)
2) Pass it via connect_args (driver-direct, supports non-string/extra options)
Copy code
python
from prefect_sqlalchemy import SqlAlchemyConnector, ConnectionComponents, SyncDriver

connector = SqlAlchemyConnector(
    connection_info=ConnectionComponents(
        driver=SyncDriver.POSTGRESQL_PSYCOPG2,
        username="user",
        password="password",
        host="localhost",
        port=5432,
        database="mydb",
    ),
    connect_args={"application_name": "my_prefect_flow"}  # <- here
)
If you prefer a raw SQLAlchemy URL string, you can also do:
Copy code
python
SqlAlchemyConnector(
    connection_info="<postgresql+psycopg2://user:password@localhost:5432/mydb?application_name=my_prefect_flow>"
)
Async note (asyncpg): you can use the query parameter as above, or via
connect_args
with server settings:
Copy code
python
# AsyncSqlAlchemyConnector(..., connect_args={"server_settings": {"application_name": "my_prefect_async"}})
Quick way to verify it’s set:
Copy code
sql
SELECT current_setting('application_name');
-- or
SELECT application_name FROM pg_stat_activity WHERE pid = pg_backend_pid();
Docs for reference: - Integration overview: prefect-sqlalchemy docs - API reference (Database): SqlAlchemyConnector API - API reference (Credentials): ConnectionComponents API Are you using
prefect_sqlalchemy.SqlAlchemyConnector
(sync) or the async connector? I can tailor the example to your exact setup.
j
@Marvin is this in the Postgres API?
m
thought for 89.9 seconds
Yes —
application_name
is a native PostgreSQL/libpq connection parameter, not Prefect-specific. - PostgreSQL docs (connection parameters): libpq connection parameters — application_name - PostgreSQL GUC (what it does/where it appears): application_name setting - psycopg2 accepts it directly (e.g.,
psycopg2.connect(application_name="...")
): psycopg2.connect docs - asyncpg supports it via
server_settings
(e.g.,
server_settings={"application_name": "..."}
): asyncpg.connect docs In Prefect (via prefect-sqlalchemy), you just pass it through to the driver: - As a URL query param (e.g.,
...?application_name=my_app
) - Or via
connect_args={"application_name": "my_app"}
If you want, I can show the exact snippet for your driver (psycopg2 vs asyncpg).
j
@Marvin I am talking about the prefect API that I am self hosting with the docker container? What do I need to set to get the application name there?
m
thought for 185.3 seconds
Got it — you’re asking about the Prefect API (Prefect Server) you’re self-hosting in Docker. To set the PostgreSQL application name for the API’s own DB connections, set this environment variable on the API container: - PREFECT_SERVER_DATABASE_SQLALCHEMY_CONNECT_ARGS_APPLICATION_NAME="your_app_name" You’ll also need to provide the DB connection URL (either of these is accepted): - PREFECT_SERVER_DATABASE_CONNECTION_URL="postgresql+asyncpg://user:pass@host:5432/prefect" - or the alias PREFECT_API_DATABASE_CONNECTION_URL="postgresql+asyncpg://user:pass@host:5432/prefect" Example docker-compose snippet:
Copy code
yaml
services:
  prefect-api:
    image: prefecthq/prefect:3-latest
    environment:
      PREFECT_API_DATABASE_CONNECTION_URL: <postgresql+asyncpg://user:password@postgres:5432/prefect>
      PREFECT_SERVER_DATABASE_SQLALCHEMY_CONNECT_ARGS_APPLICATION_NAME: "prefect-server"
      # optional: other connect args commonly used with Postgres/asyncpg
      # PREFECT_SERVER_DATABASE_SQLALCHEMY_CONNECT_ARGS_SEARCH_PATH: "public"
      # PREFECT_SERVER_DATABASE_SQLALCHEMY_CONNECT_ARGS_STATEMENT_CACHE_SIZE: "0"
Docs: - Settings reference (all server DB settings): Prefect 3 settings reference - Self-hosted guide: Self-hosting Prefect Server Note: - The
application_name
is a native PostgreSQL/libpq parameter. Prefect passes it through via SQLAlchemy connect args to asyncpg. - Works only when using Postgres (ignored for SQLite).