Skip to content

Commit bd8ba28

Browse files
committed
sets up pg log rotation
1 parent 4c6b608 commit bd8ba28

5 files changed

Lines changed: 39 additions & 10 deletions

File tree

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ services:
2222
- /var/run/docker.sock:/var/run/docker.sock:ro
2323
- /proc/:/host/proc/:ro
2424
- /sys/fs/cgroup/:/host/sys/fs/cgroup:ro
25-
- ./services/postgres/data/pg_log:/var/log/pg_log
25+
- postgres_logs:/var/log/pg_log:ro
2626
frontend:
2727
build:
2828
context: ./services/frontend
@@ -104,7 +104,7 @@ services:
104104
- dd-agent
105105
volumes:
106106
# save postgres logs to a volume for datadog to collect
107-
- './services/postgres/data/pg_log:/var/log/pg_log'
107+
- postgres_logs:/var/log/pg_log:rw
108108
environment:
109109
- POSTGRES_HOST_AUTH_METHOD=trust
110110
- POSTGRES_USER
@@ -169,7 +169,7 @@ services:
169169
"enabled": true
170170
}
171171
}]'
172-
com.datadoghq.ad.logs: '[{"source": "postgresql", "service": "postgres", "auto_multi_line_detection":true, "path": "/var/log/pg_log/postgresql_logs.json", "type": "file"}]'
172+
com.datadoghq.ad.logs: '[{"source": "postgresql", "service": "postgres", "auto_multi_line_detection":true, "path": "/var/log/pg_log/postgresql*.json", "type": "file"}]'
173173
my.custom.label.team: 'database'
174174
redis:
175175
image: redis:6.2-alpine

services/postgres/Dockerfile

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
FROM postgres:15-alpine
2+
3+
# Install dcron and su-exec for user switching
4+
RUN apk update && apk add --no-cache dcron su-exec
5+
6+
# Copy SQL dump and PostgreSQL configuration
27
COPY ./scripts/restore-2024-04-25-22-06-17.sql /docker-entrypoint-initdb.d/
3-
COPY ./postgresql.conf /postgresql.conf
4-
# create log file for postgres and give read write permission to all users
8+
COPY ./scripts/postgresql.conf /postgresql.conf
9+
10+
# Create log directory and set permissions
511
RUN mkdir -p /var/log/pg_log && \
612
chown -R postgres:postgres /var/log/pg_log && \
713
chmod -R 755 /var/log/pg_log
814

9-
CMD ["postgres", "-c", "config_file=/postgresql.conf"]
15+
# Copy the cron job file
16+
COPY ./scripts/cleanup-cron /etc/crontabs/root
17+
18+
# Copy and set the entrypoint script
19+
COPY ./scripts/start-services.sh /usr/local/bin/start-services.sh
20+
RUN chmod +x /usr/local/bin/start-services.sh
21+
22+
# Use the custom entrypoint script
23+
ENTRYPOINT ["/usr/local/bin/start-services.sh"]
24+
25+
# Default command
26+
CMD ["postgres", "-c", "config_file=/postgresql.conf"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*/1 * * * * /usr/bin/find /var/log/pg_log/ -type f -name "*" -mmin +5 -delete >> /var/log/cron.log 2>&1
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ logging_collector = on # Enable capturing of stderr and csvlo
437437
# These are only used if logging_collector is on:
438438
log_directory = '/var/log/pg_log' # directory where log files are written,
439439
# can be absolute or relative to PGDATA
440-
log_filename = 'postgresql_logs' # log file name pattern,
440+
log_filename = 'postgresql-%Y-%m-%d_%H-%M' # log file name pattern,
441441
# can include strftime() escapes
442442
log_file_mode = 0644 # creation mode for log files,
443443
# begin with 0 to use octal notation
@@ -449,7 +449,7 @@ log_truncate_on_rotation = on # If on, an existing log file with the
449449
# or size-driven rotation. Default is
450450
# off, meaning append to existing files
451451
# in all cases.
452-
log_rotation_age = '2min' # Automatic rotation of logfiles will
452+
log_rotation_age = '4min' # Automatic rotation of logfiles will
453453
# happen after that time. 0 disables.
454454
log_rotation_size = 0 # Automatic rotation of logfiles will
455455
# happen after that much log output.
@@ -524,7 +524,7 @@ log_min_duration_statement = 0 # -1 is disabled, 0 logs all statements
524524
#log_checkpoints = off
525525
#log_connections = off
526526
#log_disconnections = off
527-
log_duration = on
527+
# log_duration = on
528528
#log_error_verbosity = default # terse, default, or verbose messages
529529
#log_hostname = off
530530
log_line_prefix= '%m [%p] %d %a %u %h %c ' # special values:
@@ -556,7 +556,7 @@ log_line_prefix= '%m [%p] %d %a %u %h %c ' # special values:
556556
#log_parameter_max_length_on_error = 0 # when logging an error, limit logged
557557
# bind-parameter values to N bytes;
558558
# -1 means print in full, 0 disables
559-
log_statement = 'all' # none, ddl, mod, all
559+
#log_statement = 'all' # none, ddl, mod, all
560560
#log_replication_commands = off
561561
#log_temp_files = -1 # log temporary files equal or larger
562562
# than the specified size in kilobytes;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
# Start cron as root
4+
crond -f &
5+
6+
# Ensure data directory has correct permissions
7+
chown -R postgres:postgres /var/lib/postgresql/data
8+
chmod -R 0700 /var/lib/postgresql/data
9+
10+
# Switch to the postgres user and start PostgreSQL using the original entrypoint
11+
exec su-exec postgres docker-entrypoint.sh "$@"

0 commit comments

Comments
 (0)