在Docker容器内运行的Postgres上安装和使用pg_cron扩展

xqkwcwgp  于 2023-06-21  发布在  Docker
关注(0)|答案(4)|浏览(242)

我尝试在Docker容器内运行的Postgres上安装pg_cron,但得到此错误could not access file "pg_cron": No such file or directory。有什么解决办法吗?
基于https://stackoverflow.com/a/51797554,我尝试了以下操作:

docker-compose.yml

version: '3.7'
services:
  pg:
    container_name: pg-container
    image: postgres:11.5
    environment:
      POSTGRES_DB: "pgdb"
      POSTGRES_USER: "pguser"
      POSTGRES_PASSWORD: "pgpass"
    volumes:
      - ./:/docker-entrypoint-initdb.d
      - pgstorage
    ports:
      - "5432:5432"

volumes:
  pgstorage:

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'
cron.database_name='${POSTGRES_DB:-postgres}'
EOT
# Required to load pg_cron
pg_ctl restart

003-main.sql

CREATE EXTENSION pg_cron;
wa7juj8i

wa7juj8i1#

从我所看到的,你没有安装pg_cron任何地方。由于它没有与默认的Postgres Docker镜像打包在一起,因此您必须注意这一点。
例如,通过扩展Image并在docker-compose.yml中使用build条目。

# Dockerfile relative to docker-compose.yml

FROM postgres:11.5

RUN apt-get update && apt-get -y install git build-essential postgresql-server-dev-11

RUN git clone https://github.com/citusdata/pg_cron.git
RUN cd pg_cron && make && make install
version: '3.7'
services:
  pg:
    container_name: pg-container
    build: .
    environment:
      POSTGRES_DB: "pgdb"
      POSTGRES_USER: "pguser"
      POSTGRES_PASSWORD: "pgpass"
    volumes:
      - ./:/docker-entrypoint-initdb.d
    ports:
      - "5432:5432"

这对我很有效-它可能需要更多的优化。

yfwxisqw

yfwxisqw2#

建议的解决方案不适用于我新创建的容器。所以我是这样做的

    • Docker文件**
FROM postgres:11.5

RUN apt-get update && apt-get -y install git build-essential postgresql-server-dev-11

RUN git clone https://github.com/citusdata/pg_cron.git
RUN cd pg_cron && make && make install

RUN cd / && \
        rm -rf /pg_cron && \
        apt-get remove -y git build-essential postgresql-server-dev-11 && \
        apt-get autoremove --purge -y && \
        apt-get clean && \
        apt-get purge

COPY init-db /docker-entrypoint-initdb.d
    • init-db/www.example.compg-cron.sh
#!/usr/bin/env bash

# use same db as the one from env
dbname="$POSTGRES_DB"

# create custom config
customconf=/var/lib/postgresql/data/custom-conf.conf
echo "" > $customconf
echo "shared_preload_libraries = 'pg_cron'" >> $customconf
echo "cron.database_name = '$dbname'" >> $customconf
chown postgres $customconf
chgrp postgres $customconf

# include custom config from main config
conf=/var/lib/postgresql/data/postgresql.conf
found=$(grep "include = '$customconf'" $conf)
if [ -z "$found" ]; then
  echo "include = '$customconf'" >> $conf
fi

此外,您可以将其他init文件放入init-db目录。

    • Docker合成文件**
version: '3.7'
services:
  postgres:
    container_name: your-container
    build: .
    environment:
      POSTGRES_DB: "your_db"
      POSTGRES_USER: "your_user"
      POSTGRES_PASSWORD: "your_user"
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
volumes:
  pgdata:
    driver: local
6ovsh4lw

6ovsh4lw3#

对于那些正在寻找一个现成的形象,请尝试以下:

docker pull ramazanpolat/postgres_cron:11
mfuanj7w

mfuanj7w4#

上面的东西对我都不起作用。最后使用以下程序:

git clone https://github.com/ramazanpolat/postgres_cron.git
cd postgres_cron

在此目录中编辑Dockerfile为:

FROM postgres:15.1
LABEL MAINTAINER Michael Spitzer <professa@gmx.net>

ENV PG_CRON_VERSION "1.4.2"

RUN apt-get update && apt-get install -y --no-install-recommends \
    postgresql-server-dev-15 postgresql-contrib-15 \
    libcurl4-openssl-dev \
    wget jq cmake build-essential ca-certificates && \
    mkdir /build && \
    cd /build && \
    wget https://github.com/citusdata/pg_cron/archive/v$PG_CRON_VERSION.tar.gz && \
    tar xzvf v$PG_CRON_VERSION.tar.gz && \
    cd pg_cron-$PG_CRON_VERSION && \
    make && \
    make install && \
    cd / && \
    rm -rf /build && \
    apt-get remove -y wget jq cmake build-essential ca-certificates && \
    apt-get autoremove --purge -y && \
    apt-get clean && \
    apt-get purge && \
    rm -rf /var/lib/apt/lists/*

RUN echo "shared_preload_libraries = 'pg_cron'" >> /var/lib/postgresql/data/postgresql.conf
RUN echo "cron.database_name = '${PG_CRON_DB:-pg_cron}'" >> /var/lib/postgresql/data/postgresql.conf

COPY ./docker-entrypoint.sh /usr/local/bin/

RUN chmod a+x /usr/local/bin/docker-entrypoint.sh

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 5432
CMD ["postgres"]

在那之后

docker build -t postgres_cron:15 .

运行它:

docker run --name postgresql -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=YourPassword -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres_cron:15

YourPassword替换为您的密码。

相关问题