59

Problem

I have too many connection open using the default docker postgresql configuration

Goal

I want to extend max_connection without using a volume for mounting the configuration (I need this to be available by default for my CI environment).

I have tried to use sed to edit the configuration but this has no effect.

What is the recommended way of overriding default configuration of postgresql docker official image?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
  • This might help: [How to customize the configuration file of the official PostgreSQL Docker image?](https://stackoverflow.com/q/30848670/3776858) – Cyrus Nov 12 '17 at 18:30
  • 2
    Please see example in the documentation : https://github.com/docker-library/docs/blob/master/postgres/README.md#database-configuration – Guillaume Husta Feb 16 '22 at 21:55

7 Answers7

98

run this 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
Piero
  • 1,583
  • 10
  • 12
  • 1
    You can actually omit the `postgres` part from `command:` as the official image will [automatically forward](https://github.com/docker-library/docs/blob/master/postgres/README.md#database-configuration) any command options to `postgres`. "The entrypoint script is made so that any options passed to the docker command will be passed along to the postgres server daemon." – phoenix Jul 19 '22 at 15:44
54

It is as simple as (you just override the default CMD with postgres -N 500):

docker run -d --name db postgres:10 postgres -N 500

You can check it using:

docker run -it --rm --link db:db postgres psql -h db -U postgres

show max_connections;

max_connections 
-----------------
500
(1 row)
Sebastian
  • 16,813
  • 4
  • 49
  • 56
16

The official image provides a way to run arbitrary SQL and shell scripts after the DB is initialized by putting them into the /docker-entrypoint-initdb.d/ directory. This script:

ALTER SYSTEM SET max_connections = 500;

will let us change the maximum connection limit. Note that the postgres server will be restarted after the initializing scripts are run, so even settings like max_connections that require a restart will go into effect when your container starts for the first time.

How you attach this script to the docker container depends on how you are starting it:

Docker

Save the SQL script to a file max_conns.sql, then use it as a volume:

docker run -it -v $PWD/max_conns.sql:/docker-entrypoint-initdb.d/max_conns.sql postgres

Docker Compose

With docker compose, save the SQL script to a file max_conns.sql next to your docker-compose.yaml, and than reference it:

version: '3'
services:
  db:
    image: postgres:latest
    volumes:
      - ./max_conns.sql:/docker-entrypoint-initdb.d/max_conns.sql

Kubernetes

With kubernetes, you will need to create a configmap for the script:

kind: ConfigMap 
apiVersion: v1 
metadata:
  name: max-conns
data:
  max_conns.sql: "ALTER SYSTEM SET max_connections = 500;"

And then use it with a container:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-example
spec:
  template:
    spec:
      containers:
        - name: postgres
          image: postgres:latest
          volumeMounts:
          - name: max-conns
            mountPath: /docker-entrypoint-initdb.d
      volumes:
        - name: max-conns
          configMap:
            name: max-conns
wingedsubmariner
  • 13,350
  • 1
  • 27
  • 52
3

If you are using TestContainers do this before starting the container

  postgreSQLContainer.setCommand("postgres", "-c", "max_connections=20000");
  postgreSQLContainer.start();

From Google, Max value allowed 262143 min 1 and default 100

Youans
  • 4,801
  • 1
  • 31
  • 57
2

For containers/pods deployed on Kubernetes, max_connections can be updated by adding arguments to the entry point command from the container spec:

spec:
  containers:
  - name: postgres1
    image: postgres:15
    args: ["-c", "max_connections=1000"]

For containers deployed in the docker engine using docker run, you can pass -c max_connections=1000.

Ref: https://hub.docker.com/_/postgres

Pino
  • 7,468
  • 6
  • 50
  • 69
1

You need to develop a init script. Accept max connections value from environment variable and update it during start up from init script. Finally launch postgreSQL service

P Ekambaram
  • 15,499
  • 7
  • 34
  • 59
1

I spent a lot of time in this issue and the simplest way to resolve this is, you can add max_connections in your values.yaml file straight away. You can specify extended configuration parameters, this option will override the conf file.

For instance; extendedConfiguration: "max_connections = 500"