From 5b0af953d8a3d320cff019b7173311dd0188ef07 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 5 Jan 2022 09:36:44 -0600 Subject: [PATCH] Add Enable Logging Of Database Activity as a Postgres til --- README.md | 3 +- .../enable-logging-of-database-activity.md | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 postgres/enable-logging-of-database-activity.md diff --git a/README.md b/README.md index 3c07845..95be178 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1176 TILs and counting..._ +_1177 TILs and counting..._ --- @@ -571,6 +571,7 @@ _1176 TILs and counting..._ - [Dump And Restore A Database](postgres/dump-and-restore-a-database.md) - [Duplicate A Local Database](postgres/duplicate-a-local-database.md) - [Edit Existing Functions](postgres/edit-existing-functions.md) +- [Enable Logging Of Database Activity](postgres/enable-logging-of-database-activity.md) - [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md) - [Escaping String Literals With Dollar Quoting](postgres/escaping-string-literals-with-dollar-quoting.md) - [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md) diff --git a/postgres/enable-logging-of-database-activity.md b/postgres/enable-logging-of-database-activity.md new file mode 100644 index 0000000..cce8bc7 --- /dev/null +++ b/postgres/enable-logging-of-database-activity.md @@ -0,0 +1,43 @@ +# Enable Logging Of Database Activity + +For logging to be enabled for a PostgreSQL server, it needs to be properly +configured. This means ensuring the `logging_collector` option is on. By +default I believe it is `off`. + +This is configured in the `postgresql.conf` file and requires a server restart. + +First, to find where the conf file is. I can answer that question in a `psql` +session. + +```sql +> show config_file; + config_file +--------------------------------------------------------------------- + /Users/jbranchaud/.asdf/installs/postgres/12.3/data/postgresql.conf +(1 row) +``` + +Now, I can open up that file and search for the line that has +`logging_collector`. I uncomment that line and change `off` to `on`. + +``` +# This is used when logging to stderr: +logging_collector = on # Enable capturing of stderr and csvlog + # into log files. Required to be on for + # csvlogs. + # (change requires restart) +``` + +This requires a restart of the Postgres server. + +```bash +$ ~/.asdf/installs/postgres/12.3/bin/pg_ctl -D ~/.asdf/installs/postgres/12.3/data restart +waiting for server to shut down.... done +server stopped +waiting for server to start... + done +server started +``` + +I can now adjust any further logging-related configurations on a server or +session basis. And then view those logs.