upleveled / system-setup

Set up a PERN development environment on Windows, macOS and Linux
28 stars 19 forks source link

`postgres` logs don't appear in terminal #81

Open karlhorky opened 1 week ago

karlhorky commented 1 week ago

@Petzlc reported today that PostgreSQL is misconfigured on his machine - the original message is in German:

$ postgres
2024-07-08 11:15:44.712 CEST [29840] LOG:  Logausgabe wird an Logsammelprozess umgeleitet
2024-07-08 11:15:44.712 CEST [29840] TIPP:  Die weitere Logausgabe wird im Verzeichnis »log« erscheinen.

The same message in English, for search keywords and discoverability:

LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "log".

Our System Setup guide should not lead to logs being saved somewhere else - they should be visible in the terminal where postgres is started

Turning the logging_collector off in the postgres.conf file will solve the issue

sed -i "s/^logging_collector = on/logging_collector = off/" "$PGDATA/postgresql.conf"
ProchaLu commented 1 week ago

In the PostgreSQL docs the default log destination should be stderr

log_destination (string) PostgreSQL supports several methods for logging server messages, including stderr and syslog. On Windows, eventlog is also supported. Set this parameter to a list of desired log destinations separated by commas. The default is to log to stderr only. This parameter can only be set in the postgresql.conf file or on the server command line.

Maybe there is a difference in the postgresql.conf file that @Petzlc is using?

Eprince-hub commented 1 week ago

This also how my postgres server looks when I start it, but I think its since I did the new system setup the last time with postgres@16, never knew this was an issue

$ postgres
2024-07-09 13:17:25.766 CEST [17760] LOG:  redirecting log output to logging collector process
2024-07-09 13:17:25.766 CEST [17760] HINT:  Future log output will appear in directory "log".
Eprince-hub commented 1 week ago

I think we need to find a way to set this logging_collector to off, That should be the solution to that, according to this

Mine is on

The log_destination is already 'stderr' which should be the correct setting for this

The logging_collector is off by default so the PostgreSQL logs (which are sent to the server's standard error stream) are not handled by the logging collector process. This means that the destination of the logs depend on where the server's stderr stream is directed to.

The point is this : This means that the destination of the logs depend on where the server's stderr stream is directed to Source https://betterstack.com/community/guides/logging/how-to-start-logging-with-postgresql/

karlhorky commented 1 week ago

Maybe it's something that Chocolatey sets?

But yeah, let's turn it off in the Windows setup at least (maybe also in the macOS setup / Linux setup if we can confirm that this is something we need)

Eprince-hub commented 1 week ago

This should be the command that does that. I have now a normal log messages, after running the below command

sed -i.bak "s/^logging_collector = on/logging_collector = off/" "$PGDATA/postgresql.conf"

Before 11111111

After 11111nwenow

Error log now showing when there is anything __1111

macOS

I think the behavior is constant in macOS, it logs errors to the console there

Screenshot 2024-07-09 at 16 27 33

Error loging

Screenshot 2024-07-09 at 16 28 36

Linux

I am not even sure if there is any terminal that shows where Postgres is running on Linux. At least its what I found out from Cornelia today.

Using this command, sudo -u postgres psql moves to the psql directly and does not show anywhere postgres is running

Some commands can be run to see where Postgres is running on Linux

pg_lsclusters ps aux | grep postgres

But these show only the file path I think, it doesn't open a terminal for Postgres where errors can be logged

karlhorky commented 1 week ago

This should be the command that does that. I have now a normal log messages, after running the below command

sed -i.bak "s/^logging_collector = on/logging_collector = off/" "$PGDATA/postgresql.conf"

Sounds pretty good. Notes:

  1. What happens when we run this command with a file that doesn't have logging_collector = on in it?
  2. Where should we add this to the Windows setup guide?
    1. Are we sure this file and the $PGDATA environment variable exists at the time we're going to run it?
  3. Is there any way to simplify or make the flag more explicit for this command (the -i.bak part)

Using this command, sudo -u postgres psql moves to the psql directly and does not show anywhere postgres is running

This is not the command to start PostgreSQL on Linux though, this is running psql as the postgres user (-u flag is the "user" flag)

Eprince-hub commented 1 week ago

What happens when we run this command with a file that doesn't have logging_collector = on in it?

I don't have an official docs to back this up, but when what sed is looking for is not found, it just ignores the operation. There are some traces of if sed found something that it continues the operation, but no mention of what happens when it doesn't find what it is looking for

https://www.gnu.org/software/sed/manual/sed.html#:~:text=If%20such%20line%20is%20found

_1111

Where should we add this to the Windows setup guide? Are we sure this file and the $PGDATA environment variable exists at the time we're going to run it?

We may need to add it to be run after the first environment variables have been set. I think at this stage, we can be certain that the $PGDATA exists if the student has done the right thing

Is there any way to simplify or make the flag more explicit for this command (the -i.bak part)

Maybe I can remove this. I thought it was important that I add it so that backup of the config is created before the modification, but maybe we don't need it afterall

https://phoenixnap.com/kb/sed-replace#:~:text=the%20found%20string.-,Create%20a%20Backup,-To%20create%20a

karlhorky commented 1 week ago

Ok yeah go ahead with a PR.

There was 1 unanswered question:

Are we sure this file ... exists at the time we're going to run it?

This point was also talking about the file, not only the $PGDATA environment variable. If we're pretty sure that this file will exist, then that's a first part of the answer. 2nd part: what happens with the sed command if this file doesn't exist?

My answer below @Eprince-hub I believe that the Postgres config file should already exist.

Some reference to the file and env variable existence

https://www.postgresql.org/docs/current/storage-file-layout.html

If it doesn't for whatever reason, then there will be an error output like the one below

1111-