Search code examples
postgresqldockerecto

The number of connections to my docker postgres database container starts at 11?


I recently started learning Docker. I set up a container with a Phoenix app and migrated its database to another container with Postgres 12.4. I was getting "too many client connection errors", but this went away after I increased the limit from 10 to 100.

I went into the database and discovered that there were 11 database connections to the application's database app. I'm not sure where these connections came from, as I didn't do anything at the start of the database, except setup the database or perform migrations. What could be the source of these connections?

Here is my Ecto.Repo setup:

# Configure your database
config :discuss, Discuss.Repo,
  url: "postgres://postgres:postgres@db:5432/discuss_dev",
  username: "postgres",
  password: "postgres",
  database: "discuss_dev",
  hostname: "db",
  port: 5432,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

Here is my docker-compose.yaml:

version: '3.6'
services:
  db:
    environment:
      PGDATA: /var/lib/postgresql/data/pgdata
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DATABASE: discuss_dev
      POSTGRES_HOST_AUTH_METHOD: trust
    image: 'postgres:12-alpine'
    restart: always
    ports:
      - "11005:5432"
    expose:
      - "11005"
    volumes:
      - 'pgdata:/var/lib/postgresql/data'
  web:
    build: .
    depends_on:
      - db
    environment:
      MIX_ENV: dev
    env_file:
      - .env
    ports:
      - "11001:4000"
    expose:
      - "11001"
    volumes:
      - .:/app
volumes:
  pgdata:

My application database has 11 connections


Solution

  • Your pool size is 10, and you established another connection (presumably not through the pool) in order to see how many connections you have. If Ecto's pooler is eager in establishing connections (I don't know if it is), then it is easy to see that 10+1 should be 11.

    If you want to investigate further, pg_stat_activity will show a lot of information about each connection, like what IP address it comes from, what the application_name is, and what the current or last executed query is.