我正在尝试使用 pg_cron 设置 PostgreSQL (v16.1) Docker 容器,但在启动时出现此错误:
2024-02-12 07:15:16.320 UTC [54] ERROR: unrecognized configuration parameter "cron.database_name"
2024-02-12 07:15:16.320 UTC [54] CONTEXT: PL/pgSQL function inline_code_block line 3 at IF
2024-02-12 07:15:16.320 UTC [54] STATEMENT: CREATE EXTENSION pg_cron SCHEMA pg_catalog;
ERROR: unrecognized configuration parameter "cron.database_name"
CONTEXT: PL/pgSQL function inline_code_block line 3 at IF
我在this topic中发现了类似的问题,但所有解决方案似乎都不起作用。其他扩展(即:dblink、pg_partman、pg_jobmon)正在按预期工作。
[...]
services:
edge-postgresql:
build: ./postgres
image: edge-postgres:16.1-alpine
restart: always
environment:
POSTGRES_USER: ***
POSTGRES_PASSWORD: ***
PGDATA: /var/lib/postgresql/data
POSTGRES_MULTIPLE_DATABASES: db_1,db_2
volumes:
- ./cfg-data/:/cfg-data/
- edge-postgresql-data:/var/lib/postgresql/data
ports:
- "35432:5432"
mem_limit: 2048m
networks:
- proxy-redirect
volumes:
edge-postgresql-data:
name: edge-postgresql-data
networks:
proxy-redirect:
name: proxy-redirect
########################################################################
# POSTGRESQL
########################################################################
FROM postgres:alpine
USER root
# pg_jobmon (https://github.com/omniti-labs/pg_jobmon/tags)
ENV PG_JOBMON_VERSION v1.4.1
# pg_partman (https://github.com/pgpartman/pg_partman/tags)
ENV PG_PARTMAN_VERSION v5.0.1
# pg_cron (https://github.com/citusdata/pg_cron/tags)
ENV PG_CRON_VERSION v1.6.2
# Install pg_jobmon.
RUN set -ex \
# Get some basic deps required to download the extensions and name them fetch-deps so we can delete them later.
&& apk add --no-cache --virtual .fetch-deps \
ca-certificates \
openssl \
tar \
# Download pg_jobmon.
&& wget -O pg_jobmon.tar.gz "https://github.com/omniti-labs/pg_jobmon/archive/$PG_JOBMON_VERSION.tar.gz" \
# Make a dir to store the src files.
&& mkdir -p /usr/src/pg_jobmon \
# Extract the src files.
&& tar \
--extract \
--file pg_jobmon.tar.gz \
--directory /usr/src/pg_jobmon \
--strip-components 1 \
# Delete the src tar.
&& rm pg_jobmon.tar.gz \
# Get the depends required to build pg_jobmon and name this set of depends build-deps so we can delete them later.
&& apk add --no-cache --virtual .build-deps \
autoconf \
automake \
g++ \
clang15 \
llvm15 \
libtool \
libxml2-dev \
make \
perl \
# Change to the src.
&& cd /usr/src/pg_jobmon \
# Build the extenison.
&& make \
# Install the extension.
&& make install \
# Return to home so we are ready for the next step.
&& cd / \
# Delete the src files from this step.
&& rm -rf /usr/src/pg_jobmon
# Install pg_partman.
RUN set -ex \
# Download pg_partman.
&& wget -O pg_partman.tar.gz "https://github.com/pgpartman/pg_partman/archive/$PG_PARTMAN_VERSION.tar.gz" \
# Create a folder to put the src files in.
&& mkdir -p /usr/src/pg_partman \
# Extract the src files.
&& tar \
--extract \
--file pg_partman.tar.gz \
--directory /usr/src/pg_partman \
--strip-components 1 \
# Delete src file tar.
&& rm pg_partman.tar.gz \
# Move to src file folder.
&& cd /usr/src/pg_partman \
# Build the extension.
&& make \
# Install the extension.
&& make install \
# Delete the src files for pg_partman.
&& rm -rf /usr/src/pg_partman
# Install pg_cron.
RUN set -ex \
# Download pg_cron.
&& wget -O pg_cron.tar.gz "https://github.com/citusdata/pg_cron/archive/$PG_CRON_VERSION.tar.gz" \
# Create a folder to put the src files in.
&& mkdir -p /usr/src/pg_cron \
# Extract the src files.
&& tar \
--extract \
--file pg_cron.tar.gz \
--directory /usr/src/pg_cron \
--strip-components 1 \
# Delete src file tar.
&& rm pg_cron.tar.gz \
# Move to src file folder.
&& cd /usr/src/pg_cron \
# Build the extension.
&& make \
# Install the extension.
&& make install \
# Delete the src files for pg_partman.
&& rm -rf /usr/src/pg_cron \
# Delete the dependencies for downloading and building the extensions, we no longer need them.
&& apk del .fetch-deps .build-deps
# chown -R (recursive) username:group directory
RUN mkdir -p /docker-entrypoint-initdb.d && chown -R 70:70 /docker-entrypoint-initdb.d
USER postgres
COPY ./custom/postgresql.conf.sample /usr/local/share/postgresql/postgresql.conf.sample
COPY ./docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/
#! /bin/bash
set -e
set -u
## Creating and setting up the databases defined with $POSTGRES_MULTIPLE_DATABASES environment variable.
function create_user_and_database() {
local db=$1 file=$2
echo "Creating user and database '$db'."
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" <<-EOSQL
CREATE USER $db;
CREATE DATABASE $db;
GRANT ALL PRIVILEGES ON DATABASE $db TO $db;
EOSQL
echo "Setting up database '$db' tables."
psql -U "$POSTGRES_USER" -d $db -f './cfg-data/sql/'$file'.sql'
}
## Setting up partitioning with pg_partman.
function partitioning_management() {
local db=$1
echo "Creating pg_dblink extension."
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$db" <<-EOSQL
CREATE SCHEMA dblink;
CREATE EXTENSION dblink SCHEMA dblink;
EOSQL
# Logging extension.
echo "Creating pg_jobmon extension."
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$db" <<-EOSQL
CREATE SCHEMA jobmon;
CREATE EXTENSION pg_jobmon SCHEMA jobmon;
INSERT INTO jobmon.dblink_mapping_jobmon (username, pwd) VALUES ('$POSTGRES_USER', '$POSTGRES_PASSWORD');
EOSQL
echo "Creating pg_partman extension."
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$db" <<-EOSQL
CREATE SCHEMA partman;
CREATE EXTENSION pg_partman SCHEMA partman;
EOSQL
echo "Adding jobmon permissions."
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$db" <<-EOSQL
GRANT USAGE ON SCHEMA jobmon TO $POSTGRES_USER;
GRANT USAGE ON SCHEMA dblink TO $POSTGRES_USER;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA jobmon TO $POSTGRES_USER;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA jobmon TO $POSTGRES_USER;
GRANT ALL ON ALL SEQUENCES IN SCHEMA jobmon TO $POSTGRES_USER;
EOSQL
}
## Setting up pg_cron.
function pg_cron() {
local db="postgres"
echo "Creating pg_cron extension."
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$db" <<-EOSQL
CREATE EXTENSION pg_cron;
EOSQL
# Required to load pg_cron.
pg_ctl restart
}
## Main.
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Setting up pg_cron."
pg_cron
echo "Databases creation requested: $POSTGRES_MULTIPLE_DATABASES."
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr "," " "); do
if [ $db == "db_1" ]; then
create_user_and_database $db "db_1"
partitioning_management $db "db_1"
elif [ $db == "db_2" ]; then
create_user_and_database $db "db_2"
partitioning_management $db "db_2"
fi
done
echo "Databases created."
fi
[...]
#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------
# Add settings for extensions here
pg_partman_bgw.interval = 3600
pg_partman_bgw.role = postgres
pg_partman_bgw.dbname = 'db_1,db_2'
cron.database_name = postgres
您是否将“pg_cron”添加到 postgresql.conf 文件中的“shared_preload_libraries”?