Postgres 以编程方式打开 log_statement

发布于 2024-09-02 08:14:05 字数 141 浏览 8 评论 0 原文

我想打开修改数据库的所有 SQL 语句的日志记录。我可以通过在配置文件中设置 log_statement 标志来在我自己的计算机上获得该功能,但需要在用户的计算机上启用它。如何从程序代码中启用它? (如果重要的话,我将 Python 与 psycopg2 一起使用。)

I want to turn on logging of all SQL statements that modify the database. I could get that on my own machine by setting the log_statement flag in the configuration file, but it needs to be enabled on the user's machine. How do you enable it from program code? (I'm using Python with psycopg2 if it matters.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-09-09 08:14:05

可以通过以下方式打开修改数据库的 SQL 语句的日志记录:

ALTER SYSTEM SET log_statement TO 'mod';

-- Make it effective by triggering configuration reload (no server restart required).
SELECT pg_reload_conf();
-- To make sure the modification is not limited to the current session scope
-- it is better to log out from postgresql and log back in.

-- Check value of log_statement configuration, expected: mod
SELECT * FROM pg_settings WHERE name = 'log_statement';

这需要超级用户访问权限。

请查看以下文档链接以了解更多详细信息:

Turning on logging of SQL statements that modify the database can be achieved by:

ALTER SYSTEM SET log_statement TO 'mod';

-- Make it effective by triggering configuration reload (no server restart required).
SELECT pg_reload_conf();
-- To make sure the modification is not limited to the current session scope
-- it is better to log out from postgresql and log back in.

-- Check value of log_statement configuration, expected: mod
SELECT * FROM pg_settings WHERE name = 'log_statement';

This requires superuser access rights.

Check hereafter links to documentation for more details:

岁月无声 2024-09-09 08:14:05

“它需要在用户的计算机上启用”这句话确实令人困惑......我假设您的意思是“从用户(客户端)端”

在 Postgresql 中,一些服务器运行时参数可以从连接,但仅来自超级用户 - 并且仅适用于不需要重新启动服务器的设置。
我不确定这是否包括许多日志选项。您可以尝试使用以下内容:

SELECT set_config('log_XXX', 'off', false);

其中 log_XXX 将被相应的 日志记录设置,以及 'false' 为您要设置的值。

如果这不起作用,我想你运气不好。

The "it needs to be enabled on the user's machine" phrase is confusing, indeed... I assume you mean "from the user (client) side".

In Postgresql some server run-time parameters can be changed from a connection, but only from a superuser - and only for those settings that do not require a server restart.
I'm not sure if that includes the many log options. You might try with something like:

SELECT set_config('log_XXX', 'off', false);

where log_XXX is to be replaced by the respective logging setting, and 'false' by the value you want to set.

If that does not work, I guess you are out of luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文