Dim*_*iwa 5 postgresql sed docker
我使用默认的docker postgresql
配置打开了太多连接
我想扩展max_connection
而不使用卷来安装配置(我需要在我的CI环境中默认使用它).
我试图用来sed
编辑配置,但这没有任何效果.
覆盖postgresql docker官方映像的默认配置的推荐方法是什么?
win*_*ner 22
官方镜像提供了一种在数据库初始化后将任意 SQL 和 shell 脚本放入目录中运行它们的方法/docker-entrypoint-initdb.d/
。这个脚本:
ALTER SYSTEM SET max_connections = 500;
Run Code Online (Sandbox Code Playgroud)
让我们更改最大连接限制。请注意,postgres 服务器将在初始化脚本运行后重新启动,因此即使像这样max_connections
需要重新启动的设置也会在容器第一次启动时生效。
如何将此脚本附加到 docker 容器取决于您启动它的方式:
将 SQL 脚本保存到文件中max_conns.sql
,然后将其用作卷:
docker run -it -v $PWD/max_conns.sql:/docker-entrypoint-initdb.d/max_conns.sql postgres
Run Code Online (Sandbox Code Playgroud)
max_conns.sql
使用 docker compose,将 SQL 脚本保存到您的 旁边的文件中docker-compose.yaml
,然后引用它:
ALTER SYSTEM SET max_connections = 500;
Run Code Online (Sandbox Code Playgroud)
使用 kubernetes,您需要为脚本创建一个配置映射:
docker run -it -v $PWD/max_conns.sql:/docker-entrypoint-initdb.d/max_conns.sql postgres
Run Code Online (Sandbox Code Playgroud)
然后将其与容器一起使用:
version: '3'
services:
db:
image: postgres:latest
volumes:
- ./max_conns.sql:/docker-entrypoint-initdb.d/max_conns.sql
Run Code Online (Sandbox Code Playgroud)
Seb*_*ian 10
如(你只是覆盖默认它是那样简单CMD
用postgres -N 500
):
docker run -d --name db postgres:10 postgres -N 500
Run Code Online (Sandbox Code Playgroud)
你可以使用以下方法检查:
docker run -it --rm --link db:db postgres psql -h db -U postgres
show max_connections;
max_connections
-----------------
500
(1 row)
Run Code Online (Sandbox Code Playgroud)
运行这个docker-compose.yml
version: '2'
services:
postgres:
image: postgres:10.3-alpine
command: postgres -c 'max_connections=200'
environment:
POSTGRES_DB: pgdb
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
stdin_open: true
tty: true
ports:
- 5432:5432/tcp
Run Code Online (Sandbox Code Playgroud)