postgresql 在 Postgres/SQLAlchemy 上设置 application_name

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15685861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:51:13  来源:igfitidea点击:

Setting application_name on Postgres/SQLAlchemy

postgresqlsqlalchemypsycopg2

提问by Yaniv Aknin

Looking at the output of select * from pg_stat_activity;, I see a column called application_name, described here.

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

I see psql sets this value correctly (to psql...), but my application code (psycopg2/SQLAlchemy) leaves it blank.

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

I'd like to set this to something useful, like web.1, web.2, etc, so I could later on correlate what I see in pg_stat_activitywith what I see in my application logs.

我想将其设置为有用的东西,例如web.1web.2等,以便稍后将我看到的内容pg_stat_activity与我在应用程序日志中看到的内容相关联。

I couldn't find how to set this field using SQLAlchemy (and if push comes to shove - even with raw sql; I'm using PostgresSQL 9.1.7 on Heroku, if that matters).

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

Am I missing something obvious?

我错过了一些明显的东西吗?

回答by zzzeek

the answer to this is a combination of:

对此的答案是以下各项的组合:

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

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.

客户端库/服务器支持的任何其他连接参数都可以在连接字符串中或作为关键字传递。PostgreSQL 文档包含受支持参数的完整列表。另请注意,可以使用环境变量将相同的参数传递给客户端库。

where the variable we need is:

我们需要的变量是:

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

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 (?).

application_name 可以是任何少于 NAMEDATALEN 字符(标准版本中为 64 个字符)的字符串。它通常由应用程序在连接到服务器时设置。该名称将显示在 pg_stat_activity 视图中并包含在 CSV 日志条目中。它也可以通过 log_line_prefix 参数包含在常规日志条目中。在 application_name 值中只能使用可打印的 ASCII 字符。其他字符将替换为问号 (?)。

combined with :

结合:

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

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

基于字符串的参数可以直接从 URL 字符串作为查询参数传递:(示例...)create_engine() 还接受一个参数 connect_args,它是一个将传递给 connect() 的附加字典。这可以在需要字符串以外的类型参数时使用,并且 SQLAlchemy 的数据库连接器没有该参数的类型转换逻辑

from that we get:

从中我们得到:

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

or:

或者:

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