Sak*_*akr 83 postgresql docker
我正在使用官方的Postgres Docker图像来尝试自定义其配置.为此,我使用命令sed进行更改max_connections,例如:
sed -i -e"s/^max_connections = 100.*$/max_connections = 1000/" /var/lib/postgresql/data/postgresql.conf
Run Code Online (Sandbox Code Playgroud)
我尝试了两种方法来应用此配置.第一种方法是将命令添加到脚本并在init文件夹"/docker-entrypoint-initdb.d"中复制它.第二种方法是使用"RUN"命令在我的Dockerfile中直接运行它们(这种方法适用于非官方的Postgresql映像,配置文件的路径不同于"/ etc/postgres/...").在这两种情况下,更改都会失败,因为配置文件丢失(我认为它尚未创建).
我该如何更改配置?
编辑1:
这是用于创建图像的Dockerfile:
# Database (http://www.cs3c.ma/)
FROM postgres:9.4
MAINTAINER Sabbane <contact@cs3c.ma>
ENV TERM=xterm
RUN apt-get update
RUN apt-get install -y nano
ADD scripts /scripts
# ADD scripts/setup-my-schema.sh /docker-entrypoint-initdb.d/
# Allow connections from anywhere.
RUN sed -i -e"s/^#listen_addresses =.*$/listen_addresses = '*'/" /var/lib/postgresql/data/postgresql.conf
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf
# Configure logs
RUN sed -i -e"s/^#logging_collector = off.*$/logging_collector = on/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_directory = 'pg_log'.*$/log_directory = '\/var\/log\/postgresql'/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_filename = 'postgresql-\%Y-\%m-\%d_\%H\%M\%S.log'.*$/log_filename = 'postgresql_\%a.log'/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_file_mode = 0600.*$/log_file_mode = 0644/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_truncate_on_rotation = off.*$/log_truncate_on_rotation = on/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_rotation_age = 1d.*$/log_rotation_age = 1d/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_min_duration_statement = -1.*$/log_min_duration_statement = 0/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_checkpoints = off.*$/log_checkpoints = on/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_connections = off.*$/log_connections = on/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_disconnections = off.*$/log_disconnections = on/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^log_line_prefix = '\%t \[\%p-\%l\] \%q\%u@\%d '.*$/log_line_prefix = '\%t \[\%p\]: \[\%l-1\] user=\%u,db=\%d'/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_lock_waits = off.*$/log_lock_waits = on/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#log_temp_files = -1.*$/log_temp_files = 0/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#statement_timeout = 0.*$/statement_timeout = 1800000 # in milliseconds, 0 is disabled (current 30min)/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^lc_messages = 'en_US.UTF-8'.*$/lc_messages = 'C'/" /var/lib/postgresql/data/postgresql.conf
# Performance Tuning
RUN sed -i -e"s/^max_connections = 100.*$/max_connections = 1000/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^shared_buffers =.*$/shared_buffers = 16GB/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#effective_cache_size = 128MB.*$/effective_cache_size = 48GB/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#work_mem = 1MB.*$/work_mem = 16MB/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#maintenance_work_mem = 16MB.*$/maintenance_work_mem = 2GB/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#checkpoint_segments = .*$/checkpoint_segments = 32/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#checkpoint_completion_target = 0.5.*$/checkpoint_completion_target = 0.7/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#wal_buffers =.*$/wal_buffers = 16MB/" /var/lib/postgresql/data/postgresql.conf
RUN sed -i -e"s/^#default_statistics_target = 100.*$/default_statistics_target = 100/" /var/lib/postgresql/data/postgresql.conf
VOLUME ["/var/lib/postgresql/data", "/var/log/postgresql"]
CMD ["postgres"]
Run Code Online (Sandbox Code Playgroud)
使用此Dockerfile,构建过程会显示错误: sed: can't read /var/lib/postgresql/data/postgresql.conf: No such file or directory
Mat*_*aun 71
使用Docker Compose时,您可以command: postgres -c option=value在您docker-compose.yml的配置中使用Postgres.
例如,这使Postgres记录到一个文件:
command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs
Run Code Online (Sandbox Code Playgroud)
适应Vojtech Vitek的答案,您可以使用
command: postgres -c config_file=/etc/postgresql.conf
Run Code Online (Sandbox Code Playgroud)
更改Postgres将使用的配置文件.您将使用卷装入自定义配置文件:
volumes:
- ./customPostgresql.conf:/etc/postgresql.conf
Run Code Online (Sandbox Code Playgroud)
这docker-compose.yml是我的应用程序,展示了如何配置Postgres:
# Start the app using docker-compose pull && docker-compose up to make sure you have the latest image
version: '2.1'
services:
myApp:
image: registry.gitlab.com/bullbytes/myApp:latest
networks:
- myApp-network
db:
image: postgres:9.6.1
# Make Postgres log to a file.
# More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs
environment:
# Provide the password via an environment variable. If the variable is unset or empty, use a default password
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-4WXUms893U6j4GE&Hvk3S*hqcqebFgo!vZi}
# If on a non-Linux OS, make sure you share the drive used here. Go to Docker's settings -> Shared Drives
volumes:
# Persist the data between container invocations
- postgresVolume:/var/lib/postgresql/data
- ./logs:/logs
networks:
myApp-network:
# Our application can communicate with the database using this hostname
aliases:
- postgresForMyApp
networks:
myApp-network:
driver: bridge
# Creates a named volume to persist our data. When on a non-Linux OS, the volume's data will be in the Docker VM
# (e.g., MobyLinuxVM) in /var/lib/docker/volumes/
volumes:
postgresVolume:
Run Code Online (Sandbox Code Playgroud)
请注意,在Linux上,主机上的日志目录必须具有正确的权限.否则你会得到一个误导性的错误
致命:无法打开日志文件"/logs/postgresql-2017-02-04_115222.log":权限被拒绝
我说是误导,因为错误消息表明容器中的目录具有错误的权限,而实际上主机上的目录不允许写入.
要解决此问题,我使用了在主机上设置了正确的权限
chgroup ./logs docker && chmod 770 ./logs
Run Code Online (Sandbox Code Playgroud)
Adr*_*uat 38
postgres:9.4您继承的图像声明了一个音量/var/lib/postgresql/data.这实际上意味着您无法将任何文件复制到图像中的该路径; 更改将被丢弃.
你有几个选择:
您可以在运行时将自己的配置文件添加为卷docker run -v postgresql.conf:/var/lib/postgresql/data/postgresql.conf ....但是,我不确定它将如何与现有卷进行交互.
您可以在启动容器时复制文件.为此,将文件复制到不在卷下面的位置的构建中,然后从入口点或cmd调用脚本,该脚本将文件复制到正确的位置并启动postgres.
在Postgres官方映像后面克隆项目并编辑Dockerfile以在声明VOLUME之前添加您自己的配置文件(在运行时自动复制VOLUME指令之前添加的任何内容).
Voj*_*tek 36
默认postgresql.conf文件位于PGDATAdir(/var/lib/postgresql/data)中,这使得事情变得更加复杂,尤其是在第一次运行postgres容器时,因为docker-entrypoint.sh包装器会调用dir初始化的initdb步骤PGDATA.
要在Docker中一致地自定义PostgreSQL配置,我建议将config_filepostgres选项与Docker卷一起使用,如下所示:
docker run -d \
-v $CUSTOM_CONFIG:/etc/postgresql.conf \
-v $CUSTOM_DATADIR:/var/lib/postgresql/data \
-e POSTGRES_USER=postgres \
-p 5432:5432 \
--name postgres \
postgres:9.6 postgres -c config_file=/etc/postgresql.conf
Run Code Online (Sandbox Code Playgroud)
docker rm)docker run -d \
-v $CUSTOM_CONFIG:/etc/postgresql.conf \
-e POSTGRES_USER=postgres \
--name postgres \
postgres:9.6 postgres -c config_file=/etc/postgresql.conf
Run Code Online (Sandbox Code Playgroud)
-d从docker run命令中删除(detach选项)以直接查看服务器日志.使用psql客户端连接到postgres服务器并查询配置:
docker run -it --rm --link postgres:postgres postgres:9.6 sh -c 'exec psql -h $POSTGRES_PORT_5432_TCP_ADDR -p $POSTGRES_PORT_5432_TCP_PORT -U postgres'
psql (9.6.0)
Type "help" for help.
postgres=# SHOW all;
Run Code Online (Sandbox Code Playgroud)Yaj*_*ajo 26
当您运行正式入口点(AKA当您启动容器),它运行initdb在$PGDATA(/var/lib/postgresql/data默认设置),然后在该目录中存储这2个文件:
postgresql.conf 使用默认手动设置.postgresql.auto.conf使用ALTER SYSTEM命令自动覆盖设置.入口点还执行任何/docker-entrypoint-initdb.d/*.{sh,sql}文件.
所有这些意味着您可以在该文件夹中提供一个shell/SQL脚本,用于配置服务器以进行下一次启动(将在数据库初始化之后立即执行,或者下次启动容器时).
例:
conf.sql 文件:
ALTER SYSTEM SET max_connections = 6;
ALTER SYSTEM RESET shared_buffers;
Run Code Online (Sandbox Code Playgroud)
Dockerfile 文件:
FROM posgres:9.6-alpine
COPY *.sql /docker-entrypoint-initdb.d/
RUN chmod a+r /docker-entrypoint-initdb.d/*
Run Code Online (Sandbox Code Playgroud)
然后,您必须conf.sql在已有的数据库中手动执行.由于配置存储在卷中,因此它将在重建后继续存在.
另一种方法是-c根据需要多次传递标志:
docker container run -d postgres -c max_connections=6 -c log_lock_waits=on
Run Code Online (Sandbox Code Playgroud)
这样您就不需要构建新映像,也不需要关心已存在或不存在的数据库; 一切都会受到影响.
您可以将自定义postgresql.conf放在容器内的临时文件中,并在运行时覆盖默认配置.
要做到这一点 :
postgresql.conf在容器中复制自定义updateConfig.sh文件/docker-entrypoint-initdb.d/DockerfileFROM postgres:9.6
COPY postgresql.conf /tmp/postgresql.conf
COPY updateConfig.sh /docker-entrypoint-initdb.d/_updateConfig.sh
Run Code Online (Sandbox Code Playgroud)
updateConfig.sh#!/usr/bin/env bash
cat /tmp/postgresql.conf > /var/lib/postgresql/data/postgresql.conf
Run Code Online (Sandbox Code Playgroud)
在运行时,容器将在内部执行脚本,/docker-entrypoint-initdb.d/并使用自定义配置覆盖默认配置.
我还使用了官方映像 ( FROM postgres)\n并且我可以通过执行以下命令来更改配置。
首先是找到 PostgreSQL 配置文件。\n这可以通过在正在运行的数据库中执行此命令来完成。
\n\nSHOW config_file;\nRun Code Online (Sandbox Code Playgroud)\n\n我的情况是它返回/data/postgres/postgresql.conf。
下一步是找出正在运行的 PostgreSQL docker 容器的哈希值。
\n\ndocker ps -a\nRun Code Online (Sandbox Code Playgroud)\n\n这应该返回所有正在运行的容器的列表。就我而言,它看起来像这样。
\n\n...\n0ba35e5427d9 postgres "docker-entrypoint.s\xe2\x80\xa6" ....\n...\nRun Code Online (Sandbox Code Playgroud)\n\n现在你必须通过执行以下命令切换到容器内的 bash:
\n\ndocker exec -it 0ba35e5427d9 /bin/bash\nRun Code Online (Sandbox Code Playgroud)\n\n在容器内检查配置是否位于正确的路径并显示它。
\n\ncat /data/postgres/postgresql.conf\nRun Code Online (Sandbox Code Playgroud)\n\n我想将最大连接数从 100 更改为 1000,将共享缓冲区从 128MB 更改为 3GB。\n使用 sed 命令,我可以进行搜索并替换为配置中的相应变量。
\n\nsed -i -e"s/^max_connections = 100.*$/max_connections = 1000/" /data/postgres/postgresql.conf\nsed -i -e"s/^shared_buffers = 128MB.*$/shared_buffers = 3GB/" /data/postgres/postgresql.conf\nRun Code Online (Sandbox Code Playgroud)\n\n我们要做的最后一件事是重新启动容器内的数据库。\n找出您正在使用的 PostGres 版本。
\n\ncd /usr/lib/postgresql/\nls \nRun Code Online (Sandbox Code Playgroud)\n\n就我而言12 \n因此您现在可以通过使用正确的版本执行以下命令来重新启动数据库。
su - postgres -c "PGDATA=$PGDATA /usr/lib/postgresql/12/bin/pg_ctl -w restart"\nRun Code Online (Sandbox Code Playgroud)\n
我浏览了所有答案,然后还有另一个选择。您可以在docker文件中更改CMD值(这不是最好的方法,但是仍然可以实现目标)。
基本上我们需要
Docker文件示例:
FROM postgres:9.6
USER postgres
# Copy postgres config file into container
COPY postgresql.conf /etc/postgresql
# Override default postgres config file
CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
Run Code Online (Sandbox Code Playgroud)
尽管我认为command: postgres -c config_file=/etc/postgresql/postgresql.conf在Matthias Braundocker-compose.yml建议的文件中使用是最佳选择。
小智 5
我的解决方案适用于需要在启动 docker-entrypoint-initdb.d 之前更改配置的同事
我需要更改“shared_preload_libraries”设置,以便在工作期间 postgres 已经预加载了新库,并且 docker-entrypoint-initdb.d 中的代码可以使用它。
所以我只是在 Dockerfile 中修补了 postgresql.conf.sample 文件:
RUN echo "shared_preload_libraries='citus,pg_cron'" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "cron.database_name='newbie'" >> /usr/share/postgresql/postgresql.conf.sample
Run Code Online (Sandbox Code Playgroud)
通过此补丁,可以在 docker-entrypoint-initdb.d/ 中的 .sql 文件中添加扩展名:
CREATE EXTENSION pg_cron;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60932 次 |
| 最近记录: |