database PostgreSQL 如何查看哪些查询已经运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8208310/
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-09-08 08:23:18  来源:igfitidea点击:

PostgreSQL how to see which queries have run

databasepostgresqlpgadmin

提问by kamaci

I have a PostgreSQL DB at my computer and I have an application that runs queries on it.

我的计算机上有一个 PostgreSQL 数据库,我有一个应用程序可以在其上运行查询。

How can I see which queries has run on my DB?

如何查看我的数据库上运行了哪些查询?

I use a Linux computer and pgadmin.

我使用 Linux 计算机和 pgadmin。

回答by Erwin Brandstetter

Turn on the server log:

打开服务器日志:

log_statement = all

This will log every call to the database server.

这将记录对数据库服务器的每次调用。

I would not use log_statement = allon a production server. Produces huge log files.
The manual about logging-parameters:

我不会log_statement = all在生产服务器上使用。产生巨大的日志文件。
关于日志参数的手册:

log_statement(enum)

Controls which SQL statements are logged. Valid values are none(off), ddl, mod, and all(all statements). [...]

log_statement( enum)

控制记录哪些 SQL 语句。有效值none(关闭)ddlmodall(所有语句)。[...]

Resetting the log_statementparameter requires a server reload (SIGHUP). A restart is notnecessary. Read the manual on how to set parameters.

重置log_statement参数需要重新加载服务器 ( SIGHUP)。重新启动是没有必要的。阅读有关如何设置参数的手册

Don't confuse the server log with pgAdmin's log. Two different things!

不要将服务器日志与 pgAdmin 的日志混淆。两种不同的东西!

You can also look at the server log files in pgAdmin, if you have access to the files (may not be the case with a remote server) and set it up correctly. Have a look at: Tools -> Server status. Read more about the server status window in the manual for pgAdmin III.

如果您有权访问这些文件(远程服务器可能不是这种情况)并正确设置,您还可以在 pgAdmin 中查看服务器日志文件。看看:Tools -> Server status。在pgAdmin III手册中阅读有关服务器状态窗口的更多信息。

I prefer to read the server log files with vim(or an editor / reader of your choice).

我更喜欢使用vim(或您选择的编辑器/阅读器)阅读服务器日志文件。

回答by arvind

PostgreSql is very advanced when related to logging techniques

PostgreSql 在与日志记录技术相关时非常先进

Logs are stored in Installationfolder/data/pg_log folder. While log settings are placed in postgresql.conffile.

日志存储在Installationfolder/data/pg_log folder. 虽然日志设置放在postgresql.conf文件中。

Log format is usually set as stderr. But CSV log format is recommended. In order to enable CSV format change in

日志格式通常设置为stderr. 但建议使用 CSV 日志格式。为了启用 CSV 格式更改

log_destination = 'stderr,csvlog'   
logging_collector = on

In order to log all queries, very usefull for new installations, set min. execution time for a query

为了记录所有查询,这对新安装非常有用,请设置 min。查询的执行时间

log_min_duration_statement = 0

In order to view active Queries on your database, use

要查看数据库上的活动查询,请使用

SELECT * FROM pg_stat_activity

To log specific queries set query type

记录特定查询设置查询类型

log_statement = 'all'           # none, ddl, mod, all

For more information on Logging queries see PostgreSql Log.

有关日志查询的更多信息,请参阅PostgreSql 日志

回答by Michael

I found the log file at /usr/local/var/log/postgres.log on a mac installation from brew.

我在 brew 的 mac 安装上的 /usr/local/var/log/postgres.log 中找到了日志文件。

回答by jmunsch

While using Djangowith postgres 10.6, logging was enabled by default, and I was able to simply do:

在postgres 10.6 中使用Django时,默认情况下启用了日志记录,我可以简单地执行以下操作:

tail -f /var/log/postgresql/*

Ubuntu 18.04, django 2+, python3+

Ubuntu 18.04、django 2+、python3+

回答by s21s

You can see in pg_log folder if the log configuration is enabled in postgresql.conf with this log directory name.

您可以在 pg_log 文件夹中查看是否在 postgresql.conf 中使用此日志目录名称启用了日志配置。