postgresql 在终端中显示 Postgres 服务器日志输出并同时记录到日志中

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

Display Postgres server logs output in terminal and record to logs at same time

postgresql

提问by Pat

I'm running Postgres 9.1 (Homebrew installation on Mac OSX) and I'd like to monitor my postgres server more closely.

我正在运行 Postgres 9.1(Mac OSX 上的 Homebrew 安装),我想更密切地监视我的 postgres 服务器。

My question relates to logs. I'd like to get the logs displaying in a terminal pane. Here's what the Postgres docssay about the logs:

我的问题与日志有关。我想在终端窗格中显示日志。以下是 Postgres文档对日志的描述:

"On Unix-like systems, by default, the server's standard output and standard error are sent to pg_ctl's standard output (not standard error). The standard output of pg_ctl should then be redirected to a file or piped to another process such as a log rotating program like rotatelogs; otherwise postgres will write its output to the controlling terminal (from the background) and will not leave the shell's process group. On Windows, by default the server's standard output and standard error are sent to the terminal. These default behaviors can be changed by using -l to append the server's output to a log file. Use of either -l or output redirection is recommended."

“在类 Unix 系统上,默认情况下,服务器的标准输出和标准错误会发送到 pg_ctl 的标准输出(不是标准错误)。然后 pg_ctl 的标准输出应该被重定向到一个文件或通过管道传输到另一个进程,例如日志像rotatelogs这样的旋转程序;否则postgres会将其输出写入控制终端(从后台)并且不会离开shell的进程组。在Windows上,默认情况下,服务器的标准输出和标准错误会发送到终端。这些默认行为可以通过使用 -l 将服务器的输出附加到日志文件来更改。建议使用 -l 或输出重定向。”

So, when I get my postgres server running with the following:

因此,当我使用以下内容运行我的 postgres 服务器时:

pg_ctl start -D /usr/local/var/postgres

The logs display in the terminal window. When I run:

日志显示在终端窗口中。当我运行时:

pg_ctl start -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log

the logs go to my logfile and don't display in terminal.

日志进入我的日志文件,不会显示在终端中。

In short, it would be great if anyone can tell me what command I use after I've directed logs to the file (with the second command) to make the logs also appear at the command line. It helps when I'm developing (in Django) to watch the SQL statements get executed in real time.

简而言之,如果有人能告诉我在将日志定向到文件(使用第二个命令)后我使用了什么命令使日志也出现在命令行中,那就太好了。当我在开发中(在 Django 中)观看 SQL 语句被实时执行时,它会有所帮助。

回答by Eelke

You could watch the log with the command:

您可以使用以下命令查看日志:

tail -f /usr/local/var/postgres/server.log

回答by rbashish

I was able to find the logs in:

我能够在以下位置找到日志:

less /var/log/postgresql/postgresql-10-main.log

using ubuntu 18.04with postgresql version: 10

使用ubuntu 18.04postgresql version: 10