gpt4 book ai didi

postgresql - 在 Postgres/SQLAlchemy 上设置 application_name

转载 作者:行者123 更新时间:2023-11-29 11:07:24 24 4
gpt4 key购买 nike

查看 select * from pg_stat_activity; 的输出,我看到一个名为 application_name 的列,描述了 here .

我看到 psql 正确设置了这个值(到 psql...),但我的应用程序代码 (psycopg2/SQLAlchemy) 将它留空。

我想将它设置为一些有用的东西,比如 web.1web.2 等,这样我以后就可以关联我在 pg_stat_activity 与我在我的应用程序日志中看到的内容。

我找不到如何使用 SQLAlchemy 设置此字段(如果紧要关头 - 即使使用原始 sql;如果重要的话,我在 Heroku 上使用 PostgresSQL 9.1.7)。

我是否漏掉了一些明显的东西?

最佳答案

这个问题的答案是:

http://initd.org/psycopg/docs/module.html#psycopg2.connect

Any other connection parameter supported by the client library/server can be passed either in the connection string or as keywords. The PostgreSQL documentation contains the complete list of the supported parameters. Also note that the same parameters can be passed to the client library using environment variables.

我们需要的变量是:

http://www.postgresql.org/docs/current/static/runtime-config-logging.html#GUC-APPLICATION-NAME

The application_name can be any string of less than NAMEDATALEN characters (64 characters in a standard build). It is typically set by an application upon connection to the server. The name will be displayed in the pg_stat_activity view and included in CSV log entries. It can also be included in regular log entries via the log_line_prefix parameter. Only printable ASCII characters may be used in the application_name value. Other characters will be replaced with question marks (?).

结合:

http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html#custom-dbapi-args

String-based arguments can be passed directly from the URL string as query arguments: (example...) create_engine() also takes an argument connect_args which is an additional dictionary that will be passed to connect(). This can be used when arguments of a type other than string are required, and SQLAlchemy’s database connector has no type conversion logic present for that parameter

从中我们得到:

e = create_engine("postgresql://scott:tiger@localhost/test?application_name=myapp")

或:

e = create_engine("postgresql://scott:tiger@localhost/test", 
connect_args={"application_name":"myapp"})

关于postgresql - 在 Postgres/SQLAlchemy 上设置 application_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15685861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com