在docker-entrypoint-initdb.d中创建pg_cron扩展失败 [英] creating pg_cron extension within docker-entrypoint-initdb.d fails

查看:179
本文介绍了在docker-entrypoint-initdb.d中创建pg_cron扩展失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 docker-entrypoint-initdb中创建 pg_cron 扩展名.d / init.sql 文件,则Docker映像无法运行,并且 docker日志< id> 只是说没有这样的容器。以下是相关的.sql代码段:

If I create the pg_cron extension in a docker-entrypoint-initdb.d/init.sql file, the docker image fails to run and docker logs <id> just says "No such container." Here's the relevant .sql snippet:

CREATE DATABASE my_database;
\c my_database;
CREATE EXTENSION IF NOT EXISTS postgis CASCADE;
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
CREATE EXTENSION IF NOT EXISTS pg_cron CASCADE;

但是,如果我在docker run命令完成后创建pg_cron扩展名(即删除上面的最后一行)并在 docker run 完成后用 psql --file 单独运行),成功创建扩展(postgis和timescaledb)

However, if I create the pg_cron extension after the docker run command completes (i.e. remove the last line above and run it separately with psql --file after docker run completes), the extension gets created successfully (postgis and timescaledb extensions seem to be fine regardless).

是否有一个原因我无法从 docker-entrypoint-init.d <创建pg_cron扩展? / code>?有没有正确的地方?

Is there a reason I can't create the pg_cron extension from docker-entrypoint-init.d? Is there a correct place?

我的docker run命令如下:

My docker run command is as follows:

 docker run -d \
        --name my_container --rm \
        -p 5432:5432 \
        -clog_line_prefix="%m [%p]: [%l-1] %u@%d" \
        -clog_error_verbosity=VERBOSE \
        -cshared_preload_libraries='timescaledb,pg_cron' \
        -ccron.database_name='my_database'


推荐答案

pg_cron 只能以共享库。您必须在 postgres.conf 文件中指定它。由于 docker-entrypoint-init.d 中的所有脚本都是在启动postgres服务器之后执行的(使用 pg_ctl start ),重新启动后,对 postgres.conf 中的 shared_preload_libraries 的所有更改都可以使用(通过 pg_ctl restart )。

pg_cron can be loaded only as shared library. You must specify it in postgres.conf file. Since all scripts in docker-entrypoint-init.d are executed after postgres server is started (with pg_ctl start), all changes to shared_preload_libraries in postgres.conf can become available after restart (with pg_ctl restart).

真实世界的示例:

002-setup.sh

#!/bin/sh

# Remove last line "shared_preload_libraries='citus'"
sed -i '$ d' ${PGDATA}/postgresql.conf

cat <<EOT >> ${PGDATA}/postgresql.conf
shared_preload_libraries='pg_cron,citus'
cron.database_name='${POSTGRES_DB:-postgres}'
EOT

# Required to load pg_cron
pg_ctl restart

003-main.sql

CREATE EXTENSION pg_cron;

通知


  1. 脚本执行顺序很重要,并按文件名排序

  2. pg_cron 中可用使用 cron.database_name

  1. script execution order matters and is ordered by file names
  2. pg_cron becomes available in db specified with cron.database_name

这篇关于在docker-entrypoint-initdb.d中创建pg_cron扩展失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆