43

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
  • 230,875
  • 49
  • 477
  • 553
Dimitri Kopriwa
  • 10,978
  • 17
  • 84
  • 169
  • 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
  • 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

6 Answers6

73

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,028
  • 7
  • 8
40

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
  • 15,765
  • 4
  • 46
  • 54
7

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
  • 12,903
  • 1
  • 26
  • 48
1

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,288
  • 1
  • 29
  • 55
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"

0

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
  • 12,236
  • 4
  • 28
  • 52