在Postgres / SQLAlchemy上设置application_name [英] Setting application_name on Postgres/SQLAlchemy

查看:502
本文介绍了在Postgres / SQLAlchemy上设置application_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 select * from pg_stat_activity的输出; ,我看到名为 application_name 的列,描述了此处

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

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

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

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

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_activity with what I see in my application logs.

我找不到如何使用SQLAlchemy设置此字段的方法(并且即使推入也要推入-即使使用原始sql;如果重要,我也会在Heroku上使用PostgresSQL 9.1.7)。

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

我缺少明显的东西吗?

推荐答案

答案是

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


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

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.

其中我们需要的变量是:

where the variable we need is:

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


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

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/ zh_cn / rel_0_8 / core / engines.html#custom-dbapi-args


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

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"})

这篇关于在Postgres / SQLAlchemy上设置application_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆