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

查看:29
本文介绍了在 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.1web.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).

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

Am I missing something obvious?

推荐答案

这个问题的答案是:

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/en/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天全站免登陆