-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-pg-cron.sh
More file actions
68 lines (56 loc) · 2.41 KB
/
init-pg-cron.sh
File metadata and controls
68 lines (56 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
set -e
echo "Configuring pg_cron..."
# Always use postgres as the database for pg_cron background worker
# This is required by pg_cron, but you can schedule jobs in other databases using cron.schedule_in_database()
echo "Setting up pg_cron to run in the 'postgres' database"
# Wait for PostgreSQL config directory to be ready
while [ ! -d "${PGDATA}" ]; do
echo "Waiting for PGDATA directory to be ready..."
sleep 1
done
conf="${PGDATA}/postgresql.conf"
echo "Configuring pg_cron in: ${conf}"
# Function to add or update a PostgreSQL configuration parameter
update_conf() {
local param="$1"
local value="$2"
local search_pattern="^#*\s*${param}\s*=.*"
if grep -q "$search_pattern" "$conf"; then
# Parameter exists (commented or uncommented), update it
sed -i "s@${search_pattern}@${param} = ${value}@" "$conf"
echo "Updated ${param} = ${value}"
else
# Parameter doesn't exist, add it in the appropriate section
echo "" >> "$conf"
echo "# Added by pg_cron configuration" >> "$conf"
echo "${param} = ${value}" >> "$conf"
echo "Added ${param} = ${value}"
fi
}
# Check if pg_cron is already configured
if ! grep -q "pg_cron configuration" "$conf" 2>/dev/null; then
echo "Adding pg_cron configuration to postgresql.conf..."
# Backup the original file
cp "$conf" "${conf}.backup"
echo "Created backup at ${conf}.backup"
# Update the configuration parameters
update_conf "shared_preload_libraries" "'pg_cron,hll,pg_stat_statements'"
update_conf "cron.database_name" "'postgres'" # This must be 'postgres'
update_conf "cron.use_background_workers" "on"
update_conf "max_worker_processes" "20"
update_conf "cron.host" "''"
echo "Configuration added. New pg_cron related settings:"
grep -i "cron\|shared_preload" "$conf"
echo "PostgreSQL configuration updated for pg_cron."
echo ""
echo "IMPORTANT: pg_cron background worker must run in the 'postgres' database,"
echo "but you can schedule jobs in other databases using cron.schedule_in_database()."
echo ""
echo "Example for scheduling in another database:"
echo "SELECT cron.schedule_in_database('job_name', '*/5 * * * *', 'SELECT 1', 'other_database');"
else
echo "pg_cron configuration already exists in postgresql.conf"
fi
echo "Configuration complete. Full postgresql.conf contents:"
cat "$conf"