postgresql – 在Postgres/SQLAlchemy上设置application_name

前端之家收集整理的这篇文章主要介绍了postgresql – 在Postgres/SQLAlchemy上设置application_name前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
看看pg_stat_activity的select *的输出,我看到一个名为application_name的列,描述为 here

我看到psql正确设置这个值(对于psql …),但是我的应用程序代码(psycopg2 / sqlAlchemy)将其留空。

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

我找不到如何使用sqlAlchemy设置此字段(如果push来推动 – 即使使用raw 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 07001. 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"})

猜你在找的Postgre SQL相关文章